1.对象转数组
let obj={
"北京市": 150,
"安徽省": 145,
"上海市": 86,
"四川省": 46,
"陕西省": 42,
"湖北省": 41,
"台湾省": 40,
}
function convertToArray(objct) {
return Object.keys(objct).map(key => ({name:key,number:objct[key]}));
}
let arr=convertToArray(obj);
console.log(arr)
2.数组转对象
let arr = [
{
"type": "北京市",
"number": 150
},
{
"type": "安徽省",
"number": 145
}
]
let obj = {}
arr.forEach(item=> {
obj[item.type] = item.number
})
console.log('obj', JSON.stringify(obj, null, 2));
3.数组格式添加属性
let _arr = []
arr.map((item, index) => {
_arr.push(Object.assign({},item,{flag: false}))
})
let array=["要闻","综合","生活","卫生","教育","军事","国际","社会","经济","体育","文化","科技","时政","农业","法制","国内"]
let arr=[]
array.forEach((item,index) => {
arr.push(Object.assign({ id: index,value: item }));
});
console.log(arr);
...
4.数组改为键值对
let a=[{id:1,type:1},{id:2,type:2},{id:3,type:1},{id:4,type:2},{id:5,type:1}]
const reginObj={}
a.forEach(item=>{
reginObj[item.id]=item.type
})
let c=reginObj
console.log(c);
5.修改数组内容
this.cities=[{id:1,type:true},{id:2,type:true},{id:3,type:true}]
let newObj = [];
this.cities.map(function(item, index) {
var sevm = {};
sevm["id"] = item.id;
sevm["type"] = 2;
newObj[index] = sevm;
});
this.sourceMap = newObj;
]
6.取键值对对象的value,及转化为数组
let obj = {'11':[1,2],'22':[3,4],'33':[5,6]};
let arrData = [];
arrData=Object.values(obj)
console.log(arrData);
let arr_data = [];
arrData.map(function(value, index, array) {
arr_data = arr_data.concat(value);
});
console.log(arr_data);
7.合并两个对象map
let a={1:[1,2,3,4]}
let b={2:[5,6,7,8]}
for (var key in b) {
if (b.hasOwnProperty(key) === true) {
a[key] = b[key];
}
}
console.log(a,"1111111");
8.将对象转为key=值&key=值格式
let obj = { id: '01', name: '张三', job: '项目经理' }
let arr = []
for (let key in obj) {
arr.push(`${key}=${obj[key]}`)
}
let str = arr.join('&')
console.log(str)
9.将key=值&key=值格式转为对象
let str = 'id=01&name=张三&job=项目经理'
let arr = str.split('&')
let obj = {}
arr.forEach(function(item) {
item = item.split('=')
obj[item[0]] = item[1]
})
console.log(obj)
10.字符串与数组的互相转化
let a='abdcjw'
console.log(a.split())
let b='asd,asd';
console.log(b.split())
let a="1,2,3,4,5,6"
console.log(a.split(","))
let haha = [..."Hello"];
console.log(haha);
let arr=['1', '2', '3', '4', '5', '6']
console.log(arr.join(','))
let arr=[{name:'aaa'},{name:'bbb'},{name:'ccc'}]
getTextByJs(arr) {
var str = "";
for (var i = 0; i < arr.length; i++) {
str += arr[i].name + ",";
}
if (str.length > 0) {
str = str.substr(0, str.length - 1);
}
return str;
},
let a=this.getTextByJs(arr)
console.log(a);
11.修改对象中的某个值
1.对象的合并覆盖(Object.assign)
let type='twSource'
let obj={}
obj[type]=2
console.log(obj)
let oldObj={'id':'1','twSource':'0'}
Object.assign(oldObj, obj);
console.log(oldObj)
2.直接修改对象,数组------VUE(this.$set())
let oldObj={'id':'1','twSource':'0'}
this.$set(oldObj,'twSource',2)
console.log(oldObj)
var arr = [1, 2, 3];
this.$set(arr, 0, 4);
console.log(arr);
12.将两个数组进行对比,获取添加和删除部分,转化为键值对对象
compareArrayDiff(oldArr, newArr) {
const tNewArr = newArr || [];
const tOldArr = oldArr || [];
const totalArr = [...new Set(tNewArr.concat(tOldArr))];
const delArr = [];
totalArr.forEach((item) => {
if (!tNewArr.includes(item)) delArr.push(item);
});
const addArr = [];
totalArr.forEach((item) => {
if (!tOldArr.includes(item)) addArr.push(item);
});
let obj = {};
obj["0"] = addArr;
obj["1"] = delArr;
return {
obj,
delArr,
addArr,
};
},
let objArr = this.compareArrayDiff(arr1, arr2);
console.log(objArr.obj);
console.log(objArr.delArr);
console.log(objArr.addArr);
13.将数组根据label相同的对象放到相邻位置
conversionAdjacent(data) {
const groupedData = data.reduce((acc, item) => {
acc[item.classification] = acc[item.classification] || [];
acc[item.classification].push(item);
return acc;
}, {});
return Object.values(groupedData).flat();
},