我想更改此对象数组
const arr = [
{title: "Title 1", content: ["Lorem", "ipsum", "dolor"]},
{title: "Title 2", content: ["Lorem", "ipsum", "dolor"]},
{title: "Title 3", content: ["Lorem", "ipsum", "dolor"]},
{title: "Title 4", content: ["Lorem", "ipsum", "dolor"]}
]
以这种格式
const newArr = [
{
title: "<Label color='blue' content='Title 1' />",
content: [
"<Message content='Lorem'/>",
"<Message content='ipsum'/>",
"<Message content='dolor'/>"
]
},
....
....
{
title: "<Label color='blue' content='Title 4' />",
content: [
"<Message content='Lorem'/>",
"<Message content='ipsum'/>",
"<Message content='dolor'/>"
]
},
]
以防万一你想知道原因,我试图整合一个来自语义uu-react的手风琴组件。检查this ...
注意:请不要显示急速下注或将其标记为重复,因为我检查了很多问题,但它没有给出任何解决此特定问题的方法。
答案 0 :(得分:3)
我试了一下,我认为这就是你要找的东西:
const arr = [
{title: "Title 1", content: ["Lorem", "ipsum", "dolor"]},
{title: "Title 2", content: ["Lorem", "ipsum", "dolor"]},
{title: "Title 3", content: ["Lorem", "ipsum", "dolor"]},
{title: "Title 4", content: ["Lorem", "ipsum", "dolor"]}
];
const newArr = arr.map(function(obj) {
return {
title: "<Label color='blue' content='" + obj.title + "'/>",
content: obj.content.map(function(c) {
return "<Message content='" + c + "'/>";
})
};
});
console.log(newArr);
&#13;