繁体   English   中英

如何根据键:每个对象的值组合对象数组中的特定项目,同时保持顺序?

[英]How to combine specific items in an array of objects depending on the key: value of each object whilst maintaining the order?

我有一个像下面这样的对象数组。 每个对象都有永久键“类型”,根据“类型”,添加新键。

所以如果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",
        }
    },
    {
        "type": "text",
        "text": "H"
    },
    {
        "type": "text",
        "text": "E"
    },
    {
        "type": "text",
        "text": "Y"
    },
    {
        "type": "notText",
        "attrs": {
            "id": 20,
            "data": "Other",
        }
    },
    {
        "type": "text",
        "text": "="
    },
    {
        "type": "text",
        "text": "T"
    },
    {
        "type": "text",
        "text": "O"
    },
]

对象按顺序排列,因此取决于每个项目的“类型”和顺序,然后我需要像这样组合它们:

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

总之,无论何时 'type' = 'text' 然后将每个单个对象与类型 'text' 组合在一起。 如果下一个对象的类型为“notText”,则在下一个带有“type”=“text”的项目之前不要管它。 然后结合这些。

我已经问过一个与工作答案类似的以前的问题

 let arrayOfObj = [{ "type": "text", "text": "=" }, { "type": "text", "text": " " }, { "type": "text", "text": "S" }, { "type": "text", "text": "O" }, { "type": "notText", "attrs": { "id": 20, "data": "Something", } }, { "type": "text", "text": "H" }, { "type": "text", "text": "E" }, { "type": "text", "text": "Y" }, { "type": "notText", "attrs": { "id": 20, "data": "Other", } }, { "type": "text", "text": "=" }, { "type": "text", "text": "T" }, { "type": "text", "text": "O" }, ]; let newThing = arrayOfObj.reduce( (textKey, typeKey) => { if (typeKey.type === "text") { textKey[0].text += typeKey.text; } else { textKey.push({ type: typeKey.type, attrs: typeKey.attrs }); } return textKey; }, [{ type: "text", text: "" }] ); console.log(newThing);

然而,它结合了所有 'type' = 'text' 的实例,无论顺序或其他对象与 'type' = 'notText'。

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

我建议再次使用Array.reduce ,从一个空数组开始。

如果我们构建的数组中的最后一项是“文本”类型,则附加当前项,否则只需将其添加到数组中:

 const arrayOfObj = [ { "type": "text", "text": "=" }, { "type": "text", "text": " " }, { "type": "text", "text": "S" }, { "type": "text", "text": "O" }, { "type": "notText", "attrs": { "id": 20, "data": "Something", } }, { "type": "text", "text": "H" }, { "type": "text", "text": "E" }, { "type": "text", "text": "Y" }, { "type": "notText", "attrs": { "id": 20, "data": "Other", } }, { "type": "text", "text": "=" }, { "type": "text", "text": "T" }, { "type": "text", "text": "O" }, ] const result = arrayOfObj.reduce((acc, curr) => { // If the last item was of type 'text' _and_ the current item is too.. append the text if (acc[acc.length-1]?.type === 'text' && curr.type === 'text') { acc[acc.length-1].text += curr.text; } else { // Either current or last item in array was not type 'text' acc.push(curr) } return acc; }, []); console.log('Result:', result);

你可以这样做:

 arrayOfObj = [ { "type": "text", "text": "=" }, { "type": "text", "text": " " }, { "type": "text", "text": "S" }, { "type": "text", "text": "O" }, { "type": "notText", "attrs": { "id": 20, "data": "Something", } }, { "type": "text", "text": "H" }, { "type": "text", "text": "E" }, { "type": "text", "text": "Y" }, { "type": "notText", "attrs": { "id": 20, "data": "Other", } }, { "type": "text", "text": "=" }, { "type": "text", "text": "T" }, { "type": "text", "text": "O" }, ] let output = []; arrayOfObj.forEach(x => { if ( x.type == 'notText' || output.length == 0 || output[output.length - 1].type == 'notText' ) { output.push(x) } else { output[output.length - 1].text += x.text } }) console.log(output);

你可以在这里使用reduce

 const arrayOfObj = [ { type: "text", text: "=" }, { type: "text", text: " " }, { type: "text", text: "S" }, { type: "text", text: "O" }, { type: "notText", attrs: { id: 20, data: "Something" }, }, { type: "text", text: "H" }, { type: "text", text: "E" }, { type: "text", text: "Y" }, { type: "notText", attrs: { id: 20, data: "Other" }, }, { type: "text", text: "=" }, { type: "text", text: "T" }, { type: "text", text: "O" }, ]; const result = arrayOfObj.reduce((acc, curr) => { const { type, ...props } = curr; const last = acc[acc.length - 1]; if (!last || last.type !== "text" || last.type !== type) acc.push(curr); else last.text = last.text + props.text; return acc; }, []); console.log(result);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM