如何根据每个对象的键值组合对象数组



我有一个json对象数组,如下所示。每个对象都有永久键"type",并根据"type"添加新键。

因此,如果type: 'text',我们有一个新的关键字"text"。

如果是type: 'notText',则我们有一个新的关键字"attrs"。

arrayOfObj = [
{
"type": "text",
"text": "="
},
{
"type": "text",
"text": " "
},
{
"type": "text",
"text": "S"
},
{
"type": "text",
"text": "O"
},
{
"type": "notText",
"attrs": {
"id": 20,
"data": "Something",
}
}
]

根据每个项目的"类型",即如果类型:"文本",那么我需要将每个"文本"组合成一个对象,如下所示:

arrayOfObj = [
{
"type": "text",
"text": "= SO"
},
{
"type": "notText",
"attrs": {
"id": 20,
"data": "Something",
}
}
]

我知道要启动它,我可以使用

if(this.arrayOfObj.map(ed=>ed.type) === 'text') {
Object.assign({}, ...arrayOfObj);           
}

然而,这并不完全奏效,我不确定如何走得更远。

有人知道如何做到这一点吗?

下面的示例

const arrayOfObj = [ { type: "text", text: "=", }, { type: "text", text: " ", }, { type: "text", text: "S", }, { type: "text", text: "O", }, { type: "notText", attrs: { id: 20, data: "Something", }, }, ];
const output = arrayOfObj.reduce(
(a, b) => {
if (b.type === "text") {
a[0].text += b.text;
} else {
a.push({
type: b.type,
attrs: b.attrs,
});
}
return a;
},
[{ type: "text", text: "" }]
);
console.log(output);

您可以使用reduce方法来完成此操作。

arrayOfObj = [{
"type": "text",
"text": "="
},
{
"type": "text",
"text": " "
},
{
"type": "text",
"text": "S"
},
{
"type": "text",
"text": "O"
},
{
"type": "notText",
"attrs": {
"id": 20,
"data": "Something",
}
},
{
"type": "notText",
"attrs": {
"id": 22,
"data": "Something",
}
}
]
const t = arrayOfObj.reduce((acc, curr) => {
if (curr.type === "text") {
const accTypeText = acc.find((v) => v.type === "text");
if (accTypeText) {
accTypeText.text += curr.text;
return [...acc];
}
}
return [...acc, curr];
}, [])
console.log(t);

注意:map方法返回一个数组,所以if语句总是返回false

如果类型为'text'concatenate,我们可以使用Array.reduce从arrayOfObj输入创建所需的对象,否则只设置属性。

const arrayOfObj = [ { "type": "text", "text": "=" }, { "type": "text", "text": " " }, { "type": "text", "text": "S" }, { "type": "text", "text": "O" }, { "type": "notText", "attrs": { "id": 20, "data": "Something", } }, { "type": "someOtherType", "attrs": { "id": 35, "data": "Something else", } } ]

const result = Object.values(arrayOfObj.reduce((acc, { type, text, attrs }) => { 
if (!acc[type]) acc[type] = { type };
if (type === 'text') acc[type].text = (acc[type].text || '') + text;
if (type !== 'text') acc[type].attrs = attrs;
return acc;
}, {}));
console.log('Result:', result)

我正在一步一步地解决问题,我会这样做:

let arrayOfObj = [ { "type": "text", "text": "=" }, { "type": "text", "text": " " }, { "type": "text", "text": "S" }, { "type": "text", "text": "O" }, { "type": "notText", "attrs": { "id": 20, "data": "Something", } } ]
let completeString = "";
arrayOfObj.forEach(x => x.type == "text" ? completeString += x.text : "")
arrayOfObj = arrayOfObj.filter(x => x.type == "notText");
arrayOfObj.push({ "type": "text", "text": completeString })
console.log(arrayOfObj);

我就是这么做的。这与@R3tep&ikhvjs的答案使用reduce,只是没有那么优雅。

const arrayOfObj = [ { type: "text", text: "=", }, { type: "text", text: " ", }, { type: "text", text: "S", }, { type: "text", text: "O", }, { type: "notText", attrs: { id: 20, data: "Something", }, }, ];
function combineTexts(arrayOfObj) { 
let noTextArray = arrayOfObj.filter(i => i.type !== 'text');
let allTexts = arrayOfObj.filter(i => i.type === 'text')
.map(i => i.text)
.reduce((a, b) => a + b, '');
noTextArray.unshift({ type: 'text', text: allTexts });
return noTextArray;
}
console.log(combineTexts(arrayOfObj))

最新更新