1.js匹配数组中对应的值_匹配字符串跟数组中存在相同值,并取出数组中的对应元素,转载自博主 https://blog.csdn.net/weixin_39548490/article/details/111758328
栗子如下:
字符串strA 和数组arrB,要找出arrB中id符合strA的值,并生成一个新的数组
在这里插入代码片
let strA = "1,2,3,4,5"
let arrB = [{
"id": "2",
"name": "小王"
},
{
"id": "5",
"name": "小明"
},
{
"id": "8",
"name": "小红"
},
{
"id": "11",
"name": "小点"
},
{
"id": "15",
"name": "小赵"
},
{
"id": "22",
"name": "小小"
}]
let obj = {}
for(let i in arrB){
obj[arrB[i].id] = arrB[i];
}
let strArr = strA.split(',');
let newArr = [];
for(let item of strArr){
if(obj[item]){
newArr.push(obj[item]);
}
}
console.log(newArr)
————————————————
版权声明:本文为CSDN博主「weixin_39548490」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_39548490/article/details/111758328
2.两个长度相同数组,分别对应为key,value 生成新数组
在这里插入代码片
let arrCode = []
let arrDesc = []
this.settingResource.budgetCoupon.map(item => {
this.form.gainCoupon.push(item.code)
arrCode.push(item.code)
arrDesc.push(item.desc)
})
for(let i=0; i<arrCode.length; i++){
let obj = {}
for (let j = 0; j < arrDesc.length; j++) {
if (i === j) {
obj.code = arrCode[i]
obj.desc = arrDesc[j]
this.budgetForm.budgetArr.push(obj)
}
}
}
3.更改数组对象中的属性名称
在这里插入代码片
const list = [{
id: 'a',
title: 'A'
}, {
id: 'b',
title: 'B',
children: [{
id: 'c',
title: 'C'
}, {
id: 'd',
title: 'D'
}]
}]
let res = list.map(item=>{return {name:item.title,code:item.id}})
————————————————
版权声明:本文为CSDN博主「syx_12」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/syx_12/article/details/119853067