原数组对象:
[ //data的数据
{"txt":"a","codId":"1","id":1},
{"txt":"b","codId":"1","id":2},
{"txt":"c","codId":"1","id":3}
]
希望得到:
['a', 'b', 'c']
实现方法:
let time = data.map(item =>(item.txt))
console.log(time)
//控制台输出如下
//['a', 'b', 'c']
希望得到:
[ //data的数据
{"name":"a"},
{"name":"b"},
{"name":"c"}
]
实现方法:
let resion = data.map(item =>({name: item.txt}))
console.log(resion)
//控制台输出
[ //data的数据
{"name":"a"},
{"name":"b"},
{"name":"c"}
]
let resion= data.map(item =>({name: item.txt + item.id}))
console.log(resion)
//控制台输出
[ //data的数据
{"name":"a1"},
{"name":"b2"},
{"name":"c3"}
]
let resion= data.map(item =>({name: item.txt + item.id,,id:item.id}))
console.log(resion)
//控制台输出
[ //data的数据
{"name":"a1","id":1},
{"name":"b2","id":2},
{"name":"c3","id":3}
]
let list ={item:1,age:2,sex:0}
let list2 = [{text:'item',curId:1000},{text:'age',curId:2000},{text:'sex',curId:3000}]
//希望得到
{1000:1,2000:2,3000:0}
实现:
let formEdit={};
list2 .map((item)=>{//将中括号属性替换为英文
for(let i in list){
if(i == item.text){
let curId= item.curId;
formEdit[curId] = list[i];
}
}
});
console.log(formEdit)
三:将数组对象里面每个对象的某个属性替换成相同的值
如
cols:[
{a:1,b:2,c:3},
{a:3,b:1,c:3},
{a:1,b:2,c:3},
]
columns:[
{b:1},
]
希望将 b属性为1的对象里所有c属性的值置为1.否则c属性置为0
先得到:[
{a:1,b:2,c:0},
{a:3,b:1,c:0},
{a:1,b:2,c:0},
]
再得到[
{a:1,b:2,c:1},
{a:3,b:1,c:0},
{a:1,b:2,c:1},
]
方法
cols = cols.map(function(item, index, cols) {
item.c = 0;//得到第一步
columns.map((column,index) =>{
if(item.b == column.b){ item.c = 1;}
})
return item;
})