【问题标题】:Reconstructing an array of arrays and turning it into an array of objects with keys as the first array and values as the rest of the array items重建数组数组并将其转换为对象数组,其中键作为第一个数组,值作为其余数组项
【发布时间】:2022-01-06 03:48:44
【问题描述】:

问题本身可能需要编辑,但请听我说完。我有这个:

[
    ["a","b","c"],
    ["apple", "orange", "banana"],
    ["kiwi", "tomato", "avocado"],
    ["beans", "asparagus", "spinach"]
]

我需要它,让它看起来像下面这样:

[
    {"a":"apple", "b":"orange", "c":"banana"},
    {"a":"kiwi", "b":"tomato", "c":"avocado"},
    {"a":"a", "b":"asparagus", "c":"spinach"}
]

我做过这样的事情:

const rows = [
    ["a","b","c"],
    ["apple", "orange", "banana"],
    ["kiwi", "tomato", "avocado"],
    ["beans", "asparagus", "spinach"]
]

const dataObj = {};
const dataArr = [];

if (rows.length) {
        keys = rows[0];
        values = rows[0];

        rows.forEach((element, i) => {
          values = rows[i];
          keys.forEach((key, j) => {
            dataObj[key] = values[j];
            dataArr.push(dataObj);
          });
        });
      } 

无济于事,得到了这样的东西:

[
    {"a":"apple", "b":"orange", "c":"banana"},
    {"a":"apple", "b":"orange", "c":"banana"},
    {"a":"apple", "b":"orange", "c":"banana"}
]

这不是所需的输出。如果你能帮助我 - 以及我们的社区! - 这将是超级的。谢谢!

【问题讨论】:

    标签: javascript arrays object


    【解决方案1】:

    您可以使用几个 Array 函数来实现:

    • Array.shift: 选择第一个元素并将其从数组中移除
    • Array.map: 将项目转化为对象

    const rows = [
      ["a", "b", "c"],
      ["apple", "orange", "banana"],
      ["kiwi", "tomato", "avocado"],
      ["beans", "asparagus", "spinach"]
    ]
    
    const keys = rows.shift();
    const map = rows.map((item) => {
      const obj = {}
      keys.forEach((key, index) => {
        obj[key] = item[index]
      })
      return obj
    })
    
    console.log(map)

    【讨论】:

      【解决方案2】:

      如果你可以使用lodash: 看看这个sandbox

      import _ from 'lodash';
      
      const input = [
        ["a","b","c"],
        ["apple", "orange", "banana"],
        ["kiwi", "tomato", "avocado"],
        ["beans", "asparagus", "spinach"]
      ]
      
      const keys = input[0];
      const values = input.splice(1);
      
      console.log(keys);
      console.log(values);
      
      const output = values.map(row => _.zipObject(keys, row));
      console.log(output);
      /*
      [
        {"a":"apple","b":"orange","c":"banana"}, 
        {"a":"kiwi","b":"tomato","c":"avocado"}, 
        {"a":"beans","b":"asparagus","c":"spinach"}
      ]
      */
      

      【讨论】:

      • 试图在 nodejs 设置上实现这一点。我可能需要 require("lodash") - 会更新!
      【解决方案3】:

      为解决方案修改了一些代码

          const rows = [
          ["a","b","c"],
          ["apple", "orange", "banana"],
          ["kiwi", "tomato", "avocado"],
          ["beans", "asparagus", "spinach"]
      ]
      
      let dataObj = {};
      const dataArr = [];
      
      if (rows.length) {
          keys = rows[0];
          rows.forEach((row, i) => {
            if(i==0)
            return
            dataObj = {}
            keys.forEach((key, j) => {
              dataObj[key] = row[j];
            });
            dataArr.push(dataObj);
      
          });
        }
      console.log(dataArr)

      【讨论】:

      • 我认为这就是我试图用我的初始代码做的事情!谢谢你,你肯定明白我想用那个代码做什么。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      • 2023-01-26
      相关资源
      最近更新 更多