我有一个这样的物体
jsonOne = [{
"customer": {
"emailAddress": "test@gmail.com"
},
"recommendationProductDetails": [{
"productId": "4288",
"title": "Title 1",
"imageWebAddress": "http://url.com/GetImage/2956",
"webAddress": "http://url.com/",
"description": "Description 23"
}, {
"productId": "8888",
"title": "Title 8",
"imageWebAddress": "http://url.com/GetImage/2333",
"webAddress": "http://url.com/",
"description": "Description 55"
}]
},
{
"customer": {
"emailAddress": "test33@gmail.com"
},
"recommendationProductDetails": [{
"productId": "3333",
"title": "Title 33",
"imageWebAddress": "http://url.com/GetImage/333",
"webAddress": "http://url.com/",
"description": "Description 333"
}, {
"productId": "1111",
"title": "Title 111",
"imageWebAddress": "http://url.com/GetImage/111",
"webAddress": "http://url.com/",
"description": "Description 111"
}]
}
];
我需要转换成这样
jsonData = [{
"emailAddress": "test@gmail.com"
"productId": "4288",
"title": "Title 1",
"imageWebAddress": "http://url.com/GetImage/2956",
"webAddress": "http://url.com/",
"description": "Description 23"
}, {
"emailAddress": "test@gmail.com"
"productId": "8888",
"title": "Title 8",
"imageWebAddress": "http://url.com/GetImage/2333",
"webAddress": "http://url.com/",
"description": "Description 55"
}, {
"emailAddress": "test33@gmail.com"
"productId": "3333",
"title": "Title 33",
"imageWebAddress": "http://url.com/GetImage/333",
"webAddress": "http://url.com/",
"description": "Description 333"
}, {
"emailAddress": "test33@gmail.com"
"productId": "1111",
"title": "Title 111",
"imageWebAddress": "http://url.com/GetImage/111",
"webAddress": "http://url.com/",
"description": "Description 111"
}];
我需要删除“客户” 和“ recommendationProductDetails” ,并在“ recommendationProductDetails” 中附加值“ emailAddress” strong>
有人可以帮我映射这个吗,谢谢
答案 0 :(得分:1)
您可以使用reduce
const arr = jsonOne.reduce((acc, rec) => ([...acc, ...rec.recommendationProductDetails.map(it =>({ ...rec.customer, ...it }))]), [])
console.log(arr)
答案 1 :(得分:0)
您可以使用flatMap
const res = arr.flatMap(e =>
e.recommendationProductDetails.map(r => ({...e.customer, ...r}))
);
console.log(res);
<script>
const arr = [{
"customer": {
"emailAddress": "test@gmail.com"
},
"recommendationProductDetails": [{
"productId": "4288",
"title": "Title 1",
"imageWebAddress": "http://url.com/GetImage/2956",
"webAddress": "http://url.com/",
"description": "Description 23"
}, {
"productId": "8888",
"title": "Title 8",
"imageWebAddress": "http://url.com/GetImage/2333",
"webAddress": "http://url.com/",
"description": "Description 55"
}]
},
{
"customer": {
"emailAddress": "test33@gmail.com"
},
"recommendationProductDetails": [{
"productId": "3333",
"title": "Title 33",
"imageWebAddress": "http://url.com/GetImage/333",
"webAddress": "http://url.com/",
"description": "Description 333"
}, {
"productId": "1111",
"title": "Title 111",
"imageWebAddress": "http://url.com/GetImage/111",
"webAddress": "http://url.com/",
"description": "Description 111"
}]
}
];
</script>