繁体   English   中英

如何将数组中的 object 拆分为多个对象?

[英]How to split an object within an array into multiple objects?

我有一个对象数组,就像名称一样,fixedValue 是固定的,并且类别和总数在内部 fixedValue 数组的每个 object 中也是固定的。 但是“Col 2”、“Col 3”等可以是随机值,并且可以有多个。 因此,除 category 或 total 之外的所有其他键都可以命名为“FERFVCEEF erfe”或任何随机词:

originalDataSet = [
    {
        "fixedValue": [
            {
                "category": "SD",
                "total": 2,
                "Col 2": 208,
                "Col 3": 88,
                "Col 4": 4,
                ....
            },
            {
                "category": "EFG",
                "total": 1,
                "Col 2": 106,
                "Col 3": 46,
                "Col 4": 4,
                ....
            },
            {
                "category": "ERG",
                "total": 1,
                "Col 2": 107,
                "Col 3": 47,
                "Col 4": 5,
                ....
            },
            ....
        ]
    }
]

我的目标是将其转换为以下内容:

newDataSet = [{
  fixedValue: [
    {
      name: "SD",
      fixedValue: [
        { name: "Col 2", value: 208 },
        { name: "Col 3", value: 88 },
        { name: "Col 4", value: 4 },
        ...
      ]
    },
    {
      name: "EFG",
      fixedValue: [
        { name: "Col 2", value: 106 },
        { name: "Col 3", value: 46 },
        { name: "Col 4", value: 4 },
        ...
      ]
    },
    {
      name: "ERG",
      fixedValue: [
        { name: "Col 2", value: 107 },
        { name: "Col 3", value: 47 },
        { name: "Col 4", value: 5 },
        ...
      ]
    },
    ....
  ]}
]

它获取 originalDataSet 中的每个“类别”值,并将其作为一个名为“名称”的新键的值,将非“类别”或“总计”的键和键值分配给新键。

到目前为止,我有这样的事情,但我觉得它的方向是错误的:

// First work with the array in originalDataSet.fixedValue
const newObj = {};

// For each object in the fixedValue array
for (const key of originalDataSet.fixedValue) {

    // If the key does not equal to 'category' or 'total' e.g., in this example we're referring to 'Col 2', 'Col 3', and 'Col 4', though there could be more.
    if(!key['category'] && !key['total'])

        // Should yield something like this: { name: "Col 2", value: 107 }
        newObj['name']  = key;
        newObj['value'] = newObj[key];
    }
}

originalDataSet.fixedValue.foreach(
    newDataSet = [{
        fixedValue: [
            name: item.category
            fixedValue: [newObj]
        ]
    }]
}

我将如何实现 newDataSet?

You can use destructuring to isolate the category and total properties from the rest, and then use Object.entries on that rest object to map those key/value pairs to little {name, value} objects:

 const convert = arr => arr.map(({fixedValue}) => ({ fixedValue: fixedValue.map(({category, total, ...rest}) => ({ name: category, fixedValue: Object.entries(rest).map(([name, value]) => ({ name, value })) })) })); const originalDataSet = [{"fixedValue": [{"category": "SD","total": 2,"Col 2": 208,"Col 3": 88,"Col 4": 4,}, {"category": "EFG","total": 1,"Col 2": 106,"Col 3": 46,"Col 4": 4,}, {"category": "ERG","total": 1,"Col 2": 107,"Col 3": 47,"Col 4": 5,}]}] console.log(convert(originalDataSet))

此数据看起来像 JSON 您可以通过以下方法轻松转换它:

const originalDataSet = {
"fixedValue": [{
        "category": "SD",
        "total": 2,
        "Col 2": 208,
        "Col 3": 88,
        "Col 4": 4
    },
    {
        "category": "EFG",
        "total": 1,
        "Col 2": 106,
        "Col 3": 46,
        "Col 4": 4
    },
    {
        "category": "ERG",
        "total": 1,
        "Col 2": 107,
        "Col 3": 47,
        "Col 4": 5
    }
]
}

const obj = JSON.stringify(originalDataSet);
const obj1 = JSON.parse(obj)
const newObj = obj1.fixedValue.map(el => {
  const newObject={
    name:el.category,
    fixedValue:[]
  }
  newObject.fixedValue.push({name:"Col 2", value:el["Col 2"]})
  newObject.fixedValue.push({name:"Col 3", value:el["Col 3"]})
  newObject.fixedValue.push({name:"Col 4", value:el["Col 4"]})
  return newObject
 }) 

可以使用循环改进代码

暂无
暂无

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

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