我想更改此对象数组

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'/>"
                           ]
                },
               ]


 万一您想知道为什么,我正在尝试从语义UI反应集成一个手风琴组件。检出this ...

注意:当我检查了很多问题时,请不要显示仓促地投票或将其标记为重复,但是对于此特定问题它没有提供任何解决方案。

最佳答案

我试了一下,我想这就是您想要的:



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);

10-08 19:13