vue如何把多个对象的属性值合并到一个数组对象里面
问题描述:
数据格式如下,想把每一项中的skuName拿出来组成一个数组
let arr = [
{
"id": "46f7c6ba-b4ca-4843-b5d1-76399d1ac52a",
"parentId": "a80ff169-d6f6-4752-b980-fd818240410f",
"attributeName1": "颜色",
"attributeValue1": "黄色",
"attributeName2": "尺寸1",
"attributeValue2": "S",
"skuCode": "10",
"skuName": "美年达1",
"price": 10,
"isShelf": "上架"
},
{
"id": "2ea2a723-1a6c-4545-92ce-2ef07547642f",
"parentId": "a80ff169-d6f6-4752-b980-fd818240410f",
"attributeName1": "颜色",
"attributeValue1": "黄色",
"attributeName2": "尺寸1",
"attributeValue2": "M",
"skuCode": "11",
"skuName": "可乐1",
"price": 11,
"isShelf": "上架"
},
{
"id": "57bc93ad-84de-4737-8ca6-d6abe97eb9ae",
"parentId": "a80ff169-d6f6-4752-b980-fd818240410f",
"attributeName1": "颜色",
"attributeValue1": "黄色",
"attributeName2": "尺寸1",
"attributeValue2": "L",
"skuCode": "12",
"skuName": "雪碧1",
"price": 12,
"isShelf": "上架"
}
]
想组成这样的数据:
let arr2 =[
{
"id": "e34d1a87-a9ed-42ab-97ed-8b60765af929",
"parentId": "2490ad9e-96b5-4bcd-a846-076c097b2eec",
"skuName": "黄色",
"remarks": "",
"check": true
},
{
"id": "a3f2c0dd-d464-44ad-ada0-c561d2913014",
"parentId": "2490ad9e-96b5-4bcd-a846-076c097b2eec",
"skuName": "白色",
"remarks": "",
"check": false
},
{
"id": "e1754005-f794-4717-9241-887617a9866b",
"parentId": "2490ad9e-96b5-4bcd-a846-076c097b2eec",
"skuName": "红色",
"remarks": "",
"check": false
},
{
"id": "b974f217-a802-4540-b6eb-7898393acb5d",
"parentId": "2490ad9e-96b5-4bcd-a846-076c097b2eec",
"skuName": "紫色",
"remarks": "",
"check": false
}
]
答
let newArr = [];
arr.forEach(item => {
newArr.push({
"id": item.id,
"parentId": item.parentId,
"skuName": item.skuName,
"remarks": "",
"check": true
})
})
答
var arr2=[];
for(var i=0;i<arr.length;i++){
arr2[i]={id:arr[i].id,parentId:arr[i].parentId,skuName:arr[i].skuName,remarks:arr[i].remarks,check:arr[i].check}
}
console.log(arr2)
PS:话说你没remarks和check
答
使用对象的删除键值对的方式
arr.forEach(item => {
delete item.skuName
})
console.log(arr);
循环之后删除对应的键值对
答
const arr2 = arr1.map(({ id, parentId, skuName, remarks, check }) => ({ id, parentId, skuName, remarks, check }))
答
新建一个数组,然后循环原数组,push你需要的对象到新数组
答
楼主,你采纳回答是根据谁的代码字数多吗?