gpt4 book ai didi

javascript - 通过插入标签创建一个新的对象数组

转载 作者:行者123 更新时间:2023-11-30 11:30:48 27 4
gpt4 key购买 nike

我想改变这个对象数组:

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

以防万一你想知道为什么,我正在尝试从 semantic-ui-react 中集成 Accordion 组件。检查this out .

最佳答案

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

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

关于javascript - 通过插入标签创建一个新的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46321184/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com