简体   繁体   English

根据长度动态设置数组键

[英]Setting array keys dynamically based on length

Given an array in this format: 给定以下格式的数组:

[
  [{
      name: "name",
      value: "My-name"
    },
    {
      name: "qty",
      value: "1"
    },
    {
      name: "url",
      value: "test.com"
    },
    {
      name: "comment",
      value: "my-comment"
    }
  ],
  [{
      name: "name",
      value: "My-name2"
    },
    {
      name: "qty",
      value: "3"
    },
    {
      name: "url",
      value: "test2.com"
    }
  ],
  [{
      name: "name",
      value: "My-name3"
    },
    {
      name: "qty",
      value: "1"
    },
    {
      name: "url",
      value: "test3.com"
    },
    {
      name: "comment",
      value: "my-comment3"
    }
  ]
]

I'm looking to switch that to: 我希望将其切换为:

[
  [
    { name: "My-name" },
    { qty: "1" },
    { url: "test.com" },
    { comment: "my-comment", }
  ],[
    { name: "My-name2" },  
    { qty: "3" },
    { url: "test2.com",
  ],[
    { name: "My-name3", },
    { qty: "1", },
    { url: "test3.com", },
    { comment: "my-comment3", }
  ]
]

In other words, swapping out the array keys but maintaining the object structure within each array element. 换句话说,换出数组键,但在每个数组元素中维护对象结构。

I've tried looping over each element and can swap the keys out using something like: 我尝试遍历每个元素,并可以使用以下方式交换键:

 newArray[iCount][item.name] = item.value;

However I'm then struggling to preserve the object order. 但是我当时正努力保持对象顺序。 Note that the comment field may or may not appear in the object. 请注意,注释字段可能会或可能不会出现在对象中。

With Array.map() function: 使用Array.map()函数:

 var arr = [ [{name:"name",value:"My-name"},{name:"qty",value:"1"},{name:"url",value:"test.com"},{name:"comment",value:"my-comment"}], [{name:"name",value:"My-name2"},{name:"qty",value:"3"},{name:"url",value:"test2.com"}], [{name:"name",value:"My-name3"},{name:"qty",value:"1"},{name:"url",value:"test3.com"},{name:"comment",value:"my-comment3"}] ], result = arr.map(function(a){ return a.map(function(obj){ var o = {}; o[obj.name] = obj.value return o; }); }); console.log(result); 

Check my moreBetterOutput value. 检查我的moreBetterOutput值。 I think will be better. 我认为会更好。

If you still need a result like your example in the question then you can check output value. 如果您仍然需要类似问题示例的结果,则可以检查output值。

 const input = [ [ { name:"name", value:"My-name" }, { name:"qty", value:"1" }, { name:"url", value:"test.com" }, { name:"comment", value:"my-comment" } ], [ { name:"name", value:"My-name2" }, { name:"qty", value:"3" }, { name:"url", value:"test2.com" } ], [ { name:"name", value:"My-name3" }, { name:"qty", value:"1" }, { name:"url", value:"test3.com" }, { name:"comment", value:"my-comment3" } ] ] const output = input.map(arr => arr.map(obj => ({[obj.name]: obj.value}))) const moreBetterOutput = output.map(arr => arr.reduce((acc, item, index) => { acc[Object.keys(item)[0]] = item[Object.keys(item)[0]]; return acc; }, {}) ) //console.log(output); console.log(moreBetterOutput); 

Another map function: 另一个地图功能:

const result = array.map( subarray =>
 Object.assign(...subarray.map( ({name, value}) => ({ [name] : value }) ))
);

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

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