1.循环这个数组变成数组对象
var data = [ "USA", "Denmark", "London"];
var data = [ { "id" : 1, "label": "USA" }, { "id" : 2, "label": "Denmark" }, { "id" : 3, "label": "London" } ];
解决方法:
1.使用push()
数据阵列 var data = [ "USA", "Denmark", "London"];
var demArray =[];
data.forEach(function(item,index){
demArray.push(
{ id:index+1,label:item }
)
})
console.log(demArray)
2.使用map()
var data = [ "USA", "Denmark", "London"];
var demArray =[];
var demArray = data.map(item => {
return {
id:index+1,
imgurl: item
}
})
console.log(demArray)
// 数组对象相加
var list = [
{ name: "苹果", count: 20 },
{ name: "橘子", count: 20 },
{ name: "橙子", count: 20 }
];
let countTime = list.reduce((tutu, item) => {
return tutu + item.count;
}, 0);
console.log(countTime, "合并相加");
过滤两个数组并取出不同/相同项(vue/filter)
// 数组A
let arrA = ['1111','2222','333','4444','5555']
// 数组B
let arrB = [
{ id: "aaa" },
{ id: "bbb" },
{ id: "4444" },
{ id: "5555" },
{ id: "dddd" },
{ id: "eeee" },
{ id: "ffff" },
{ id: "yyyy" },
{ id: "mmmm" }
];过滤B数组中不包含A数组的项
let differentArr = arrB.filter(item => arrA.indexOf(item.id) > -1)
// differentArr =['aaa','bbb','dddd','eeee','ffff','yyyy','mmmm']
过滤B数组中包含A数组的项
let differentArr = arrB.filter(item => arrA.indexOf(item.id) != -1)
// differentArr =['4444','5555']