vue如何把多个对象的属性值合并到一个数组对象里面
论文问答
1
数据格式如下,想把每一项中的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 }) })
-
代码如下:
const arr2 = arr1.map(({ id, parentId, skuName, remarks, check }) => ({ id, parentId, skuName, remarks, check }))
发表回复