根据长度动态设置数组键

时间:2017-10-24 15:46:01

标签: javascript jquery arrays

给定这种格式的数组:

[
  [{
      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"
    }
  ]
]

我希望将其切换为:

[
  [
    { 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", }
  ]
]

换句话说,交换数组键但保持每个数组元素中的对象结构。

我已尝试在每个元素上循环,并可以使用以下内容交换密钥:

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

然而,我正努力保留对象顺序。请注意,注释字段可能会也可能不会出现在对象中。

3 个答案:

答案 0 :(得分:1)

使用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);




答案 1 :(得分:1)

检查我的moreBetterOutput值。我认为会更好。

如果您仍然需要问题中的示例结果,那么您可以检查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);




答案 2 :(得分:0)

另一个地图功能:

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