【问题标题】:Converting an Object of Arrays into Objects of Objects将数组对象转换为对象对象
【发布时间】:2020-06-15 20:35:46
【问题描述】:

目前我的数据是这样的

{
    numberOne: [{
        timestamp: 3829383,
        value: 20323.23
    }],
    numberTwo: [{
        timestamp: 2382342,
        value: 120912.1
    }]
    ...
}

我想把它变成这种格式

{
    numberOne: {
        timestamp: 3829383,
        value: 20323.23
    },
    numberTwo: {
        timestamp: 2382342,
        value: 120912.1
    }
    ...
}

我试过Object.assign.map().filter()...似乎没有任何效果!

任何关于我如何完成这项工作的想法都会非常有帮助

【问题讨论】:

  • 你的数据不能看起来像第一个例子。
  • 您的输入数据看起来不是有效的 JS 代码。
  • 您的数据不能像 [ timestamp: 3829383, value: 20323.23 ] 这样。它的语法错误
  • 你确定它看起来像这样吗?前者不是有效的 json 也不是有效的 js 对象。
  • 抱歉,是的,你说得对 - 我已经修改了代码。

标签: javascript arrays reactjs javascript-objects array.prototype.map


【解决方案1】:

枚举对象中的条目,取出每个数组中的第一项,并用键和项创建一个对象。

const data = { numberOne: [{ timestamp: 3829383, value: 20323.23 }], numberTwo: [{ timestamp: 2382342, value: 120912.1 }] }

const transform = (arr) => 
    Object.entries(arr).reduce((acc,[k,[v]]) => (acc[k] = v, acc), {})

console.log(transform(data))

或者,您可以使用Object.fromEntries

const data = { numberOne: [{ timestamp: 3829383, value: 20323.23 }], numberTwo: [{ timestamp: 2382342, value: 120912.1 }] }

const transform = (arr) => 
    Object.fromEntries(Object.entries(arr).map(([k,[v]]) => [k, v]))

console.log(transform(data))

【讨论】:

    【解决方案2】:

    您可以使用reduce 重建对象以迭代Object.entries(它为您提供一个键和值的数组):

    const data = {
      numberOne: [{
        timestamp: 3829383,
        value: 20323.23
      }],
      numberTwo: [{
        timestamp: 2382342,
        value: 120912.1
      }]
    }
    
    // Grab the entries and iterate over each
    // destructuring out the key and value from the array
    const out = Object.entries(data).reduce((acc, [key, value]) => {
    
      // Set the key on the initial object (acc), and set
      // the value to be the first element of the value array
      acc[key] = value[0];
    
      // Return the initial object (acc) for the next iteration
      return acc;
    }, {});
    
    console.log(out);

    【讨论】:

      【解决方案3】:

      您可以使用Object.entries().reduce() 和一些Array Destructuring 将输入对象转换为所需的输出:

      const data = {
        numberOne: [{ timestamp: 3829383, value: 20323.23 }],
        numberTwo: [{ timestamp: 2382342, value: 120912.1 }]
      };
      
      const result = Object.entries(data)
                           .reduce((r, [k, [v]]) => (r[k] = v, r), {});
      
      console.log(result);
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

        【解决方案4】:

        您可以使用for (var key in obj) 循环并为每个键设置所需的值:

        var obj = {
            numberOne: [{ timestamp: 3829383, value: 20323.23 }],
            numberTwo: [{ timestamp: 2382342, value: 120912.1 }]
        };
        console.log("Before -->", obj);
        
        for (var key in obj) {
           obj[key] = obj[key][0]; // take item 0 from each array
        }
        console.log("After -->", obj);

        【讨论】:

          猜你喜欢
          • 2016-03-11
          • 2022-01-20
          • 2017-01-14
          • 2019-07-11
          • 1970-01-01
          相关资源
          最近更新 更多