简体   繁体   English

在 JavaScript 中销毁数组的对象

[英]Destruct object of array in JavaScript

Hi Guys I have an array of objects, which I want to destruct.嗨,伙计们,我有一组对象,我想销毁它们。 The following is an excerpt of the array.以下是数组的摘录。

[
  {
    "Area": "Werk Produktivität [%] - Target",
    "Jan": 86.21397507374327,
    "Feb": 86.0570021973368,
    "Mrz": 88.70898346258058,
    "Apr": 85.29801908413164,
    "May": 85.07431241640211
  },
  {
    "Area": "Werk Produktivität [%] - Actual",
    "Jan": 84.17054711398421,
    "Feb": 83.80826026601528,
    "Mrz": 84.11553769971036,
    "Apr": 83.76460916731,
    "May": 82.69773876702813
  }
]

What I now try to achieve is splitting the array into the following :我现在尝试实现的是将数组拆分为以下内容:

[
  {
    "Area": "Werk Produktivität [%] - Target",
    "Jan": 86.21397507374327,

  },
  {
    "Area": "Werk Produktivität [%] - Target",
    "Feb": 86.0570021973368,
   
  },
  {
    "Area": "Werk Produktivität [%] - Target",
    "Mrz": 88.70898346258058,

  },
  {
    "Area": "Werk Produktivität [%] - Target",
    "Apr": 85.29801908413164,

  },
  {
    "Area": "Werk Produktivität [%] - Target",
    "May": 85.07431241640211,
  },
  {
    "Area": "Werk Produktivität [%] - Actual",
    "Jan": 84.17054711398421,

  },
  {
    "Area": "Werk Produktivität [%] - Actual",
    "Feb": 83.80826026601528,
  },
  {
    "Area": "Werk Produktivität [%] - Actual",
    "Mrz": 84.11553769971036,

  },
    {
    "Area": "Werk Produktivität [%] - Actual",
    "Apr": 83.76460916731,
  },
   {
    "Area": "Werk Produktivität [%] - Actual",
    "May": 82.69773876702813
  }

I was thinking of using the ...rest parameter the following way but I only get the last 5 items of the array.我正在考虑以下列方式使用...rest参数,但我只得到数组的最后 5 项。 Please note that obj is above excerpt of the object of array.请注意,obj 是数组对象的摘录。

const fn = ({ Area, ...rest }) =>
  Object.values(rest)
    .map(Month => ({
      Area,
      Month
    }))
       


})
const result = fn(obj)

Not sure what obj in result = fn(obj) does here.不确定obj in result = fn(obj)在这里做什么。 I think you are calling map on the array and getting the first item in the resulting 2D array.我认为您正在数组上调用map并获取结果二维数组中的第一项。 You just need to call flatMap on the array with fn as the callback.您只需要使用fn作为回调在数组上调用flatMap

const output = inputArray.flatMap(fn)

or或者

const output = inputArray.map(fn).flat()

Also the fn needs to be tweaked a bit. fn也需要稍微调整一下。 You need to get the entires of the rest object since you need the key of the object as well您需要获取rest对象的全部内容,因为您还需要对象的密钥

Object.entries(rest)
      .map(([key, value]) => ({ Area, [key]: value }))

 const input = [ { "Area": "Werk Produktivität [%] - Target", "Jan": 86.21397507374327, "Feb": 86.0570021973368, "Mrz": 88.70898346258058, "Apr": 85.29801908413164, "May": 85.07431241640211 }, { "Area": "Werk Produktivität [%] - Actual", "Jan": 84.17054711398421, "Feb": 83.80826026601528, "Mrz": 84.11553769971036, "Apr": 83.76460916731, "May": 82.69773876702813 } ] const fn = ({ Area, ...rest }) => Object.entries(rest) .map(([key,value]) => ({ Area, [key]: value })) const output = input.flatMap(fn) console.log(output)

you can use reduce and Object.entries for getting result same as :您可以使用reduceObject.entries来获得与以下内容相同的结果:

 const data = [ { "Area": "Werk Produktivität [%] - Target", "Jan": 86.21397507374327, "Feb": 86.0570021973368, "Mrz": 88.70898346258058, "Apr": 85.29801908413164, "May": 85.07431241640211 }, { "Area": "Werk Produktivität [%] - Actual", "Jan": 84.17054711398421, "Feb": 83.80826026601528, "Mrz": 84.11553769971036, "Apr": 83.76460916731, "May": 82.69773876702813 } ] const result = data.reduce((res, {Area, ...rest}) => { Object.entries(rest).forEach(([key, value]) => res.push({Area, [key]: value})) return res }, []) console.log(result)

You can use the function Array.prototype.reduce and inside of it the function Array.prototype.map to build the desired object.您可以使用函数Array.prototype.reduce及其内部的函数Array.prototype.map来构建所需的对象。

 const arr = [ { "Area": "Werk Produktivität [%] - Target", "Jan": 86.21397507374327, "Feb": 86.0570021973368, "Mrz": 88.70898346258058, "Apr": 85.29801908413164, "May": 85.07431241640211 }, { "Area": "Werk Produktivität [%] - Actual", "Jan": 84.17054711398421, "Feb": 83.80826026601528, "Mrz": 84.11553769971036, "Apr": 83.76460916731, "May": 82.69773876702813 }]; const result = arr.reduce((a, {Area, ...months}) => { return [...a, ...Object.entries(months).map(([month, value]) => ({Area, [month]: value}))]; }, []); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

const ary = [
  {
    "Area": "Werk Produktivität [%] - Target",
    "Jan": 86.21397507374327,
    "Feb": 86.0570021973368,
    "Mrz": 88.70898346258058,
    "Apr": 85.29801908413164,
    "May": 85.07431241640211
  },
  {
    "Area": "Werk Produktivität [%] - Actual",
    "Jan": 84.17054711398421,
    "Feb": 83.80826026601528,
    "Mrz": 84.11553769971036,
    "Apr": 83.76460916731,
    "May": 82.69773876702813
  }
];

const newAry = [];

for(item of ary){
  const {
    Area,
    ...months
  }=item;
  for([k,v] of Object.entries(months)){
    newAry.push({Area,[k]:v});
    
  }
}
console.log(newAry);

Hope this helps you!!!希望这对你有帮助!!!

You can simply achieve that by using Destructuring assignment .您可以通过使用Destructuring assignment来简单地实现这一点。

Demo :演示

 const arr = [ { "Area": "Werk Produktivität [%] - Target", "Jan": 86.21397507374327, "Feb": 86.0570021973368, "Mrz": 88.70898346258058, "Apr": 85.29801908413164, "May": 85.07431241640211 }, { "Area": "Werk Produktivität [%] - Actual", "Jan": 84.17054711398421, "Feb": 83.80826026601528, "Mrz": 84.11553769971036, "Apr": 83.76460916731, "May": 82.69773876702813 } ]; const res = []; arr.forEach(obj => { const {Area, ...remaining} = obj; Object.keys(remaining).forEach(month => { res.push({ Area, [month]: remaining[month] }) }) }) console.log(res);

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

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