【问题标题】:Is there any shorthand method to convert array of string array with header as first array to Objects of array?是否有任何速记方法可以将带有标题的字符串数组数组作为第一个数组转换为数组对象?
【发布时间】:2019-08-09 06:24:08
【问题描述】:

是否有任何速记方法可以将带有标题的字符串数组数组作为第一个数组(输入如下所示)转换为数组对象(预期输出如下所示)

使用 for 循环我们可以实现这一点,我正在寻找任何简写和优化的解决方案来做到这一点。

让我知道是否有任何简单且优化的方法来实现这一点。

输入

[
  ['fromAge', 'toAge', 'gender', 'institutionalRaf'],
  [0, 10, 'F', '1.5'],
  [11, 20, 'F', '2.5']
]

预期输出:

[{
   fromAge : 0,
   toAge: 10,
   gender: "F",
   institutionalRaf : "1.5"
},
{
   fromAge : 11,
   toAge: 20,
   gender: "F",
   institutionalRaf : "2.5"
}
...
]

【问题讨论】:

  • 请张贴原始数据而不是图片
  • 如果可以,请将您的实际代码作为文本编辑到您的问题中 - 代码单独的图像是tedious and difficult,可以使用和调试.它会迫使那些本来想帮助你的人先transcribe your image,这是浪费时间。

标签: javascript arrays json


【解决方案1】:

您可以使用mapreudce

  • 取第一个元素为header,其余元素为values
  • 循环遍历values 数组,为每个元素构建一个对象,其键来自header,值来自element

let data = [["fromAge","toAge","gender","institutionalRaf"],["1",'4','m','12'],["4",'12','f','22'],["10",'20','m','109']]
let [header,...values] = data

let final = values.map(v=> {
  return v.reduce((op,inp,index)=>{
    op[header[index]] = inp
    return op
  },{})
})

console.log(final)

【讨论】:

    【解决方案2】:

    您可以将键和值分开,并将值作为对象与键映射。

    var array = [['fromAge', 'toAge', 'gender', 'institutionalRaf'], [0, 10, 'F', '1.5'], [11, 20, 'F', '2.5']],
        [keys, ...values] = array,
        result = values.map(a => Object.assign(...keys.map((k, i) => ({ [k]: a[i] }))));
    
    console.log(result);

    【讨论】:

      【解决方案3】:

      我会移出第一个键数组,然后使用 .map 创建条目并使用 Object.fromEntries 创建对象:

      const arr = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        [4, 5, 6]
      ];
      
      const keys = arr.shift();
      const output = arr.map(values =>
        Object.fromEntries(
          values.map((value, i) => [keys[i], value])
        )
      );
      
      console.log(output);

      Object.fromEntries 是一种相对较新的方法。在旧环境中,要么使用 polyfill,要么使用 reduce 创建对象:

      const arr = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        [4, 5, 6]
      ];
      
      const keys = arr.shift();
      const output = arr.map(values => (
        values.reduce((a, value, i) => {
          a[keys[i]] = value;
          return a;
        }, {})
      ));
      
      console.log(output);

      【讨论】:

        【解决方案4】:

        如果键是固定的,我们可以使用下面的简单方法

        let arr=[
          ['fromAge', 'toAge', 'gender', 'institutionalRaf'],
          [0, 10, 'F', '1.5'],
          [11, 20, 'F', '2.5']
        ];
        let arr1=arr.slice();
        let x=arr1.shift();
        let x1=arr1.map(a=>(
                            {
                              [x[0]]:a[0],
                              [x[1]]:a[1],
                              [x[2]]:a[2],
                              [x[3]]:a[3],
                              
                            }
                           )
                         )
        console.log(x1);

        【讨论】:

          【解决方案5】:

          使用destructuringmapreduce

          const array = [
            ['fromAge', 'toAge', 'gender', 'institutionalRaf'],
            [0, 10, 'F', '1.5'],
            [11, 20, 'F', '2.5']
          ]
          const [keys, ...values] = array
          const result = values.map((value) => value.reduce((a, b, index) => ({...a, [keys[index]]: b}), {}), [])
          console.log("result",result)

          【讨论】:

            猜你喜欢
            • 2021-05-09
            • 1970-01-01
            • 1970-01-01
            • 2020-12-19
            • 1970-01-01
            相关资源
            最近更新 更多