通过插入标记创建新的对象数组

时间:2023-01-05 22:57:25

I want to change this array of objects

我想改变这个对象数组

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"]}
]


to this format

这种格式

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


Just in case you want to know why, I'm trying to integrate an accordian component from semantic-ui-react. Check this out...

如果您想知道原因,我正在尝试整合来自语义uu-react的手风琴组件。看一下这个...

Note: Please don't show haste to downvote or mark it as duplicate as I checked a lot of questions but it did not give any solution to this particular problem.

注意:请不要显示急速下注或将其标记为重复,因为我检查了很多问题,但它没有给出任何解决此特定问题的方法。

1 个解决方案

#1


3  

I gave it a shot and I think this is what you're looking for:

我试了一下,我想这就是你要找的东西:

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

#1


3  

I gave it a shot and I think this is what you're looking for:

我试了一下,我想这就是你要找的东西:

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