比如数据格式如下
let arr1 = [
{
"specName": "颜色",
"specItems": [
{
"specItem": "黄色",
"image": ""
},
{
"specItem": "白色",
"image": ""
},
{
"specItem": "红色",
"image": ""
}
],
"productspecId": "2490ad9e-96b5-4bcd-a846-076c097b2eec",
"value": "attributeName1",
"isCheck": false
},
]
let arr2 = [
{
"value": "黄色",
"checked": false
},
{
"value": "白色",
"checked": false
},
{
"value": "红色",
"checked": false
},
{
"value": "紫色",
"checked": false
}
]
我想比较arr1中specItems里面每一个specItem和arr2中每一个value相不相等并把其中相等的arr2对应的checked变成true,求详细解释
let newArray = []
for (let i = 0; i < this.tableCategoryList.length; i++) {
const element = this.tableCategoryList[this.clickProducIndex].specItems;
for (let o = 0; o < this.dynamicTags.length; o++) {
const element2 = this.dynamicTags[o].value;
if (element == element2 ) {
console.log(true)
}else{
console.log('false')
}
}
}
这是我写的,只比较出了两个值相不相等
是一对一吗?
const { specItems } = arr1[0];
arr2.forEach((cur, index) => {
specItems[index] && cur.value === specItems[index].specItem
? (cur.checked = true)
: null;
});
console.log(arr1, arr2);
arr2.forEach(v=>{arr1[0].specItems.find(v1=>{if(v1.specItem===v.value){v.checked=true;}})})