数组到对象的数组 - javascript [英] Array of array to object - javascript

查看:119
本文介绍了数组到对象的数组 - javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在循环将数组的数组转换为对象,但最终的对象只有对象中的最后一项。我感到困惑,因为你不能推入一个像数组的对象,循环的数量让我感到沮丧。需要帮助

这里是JSbin: http://jsbin.com/torawovicu/edit?js,console



另外如何获取与数组相同的对象?



结果应该如下所示:

  var newResult = 
{
{itemcode:1,item:'Pen','cashier':'Sam'},
{itemcode:2,item:'Eraser','cashier':'Kim'}

$ / code $ / pre

这是我的代码

< pre-class =snippet-code-js lang-js prettyprint-override> var list = [['itemCode',1],['item','Pen'],['cashier' ,'Sam']],[['itemCode',2],['item','Eraser'],['cashier','Kim']]] // console。 log(people.length);函数结果(数组){var newObj = {}; var newArr = []; for(var x in array){//console.log(array [x])var item = array [x]; for(var y in item){var itemSingle = item [y] //console.log (itemSingle);对于(i = 0; i

您必须使用一个循环遍历主数组,然后运行循环遍历每个数组项(也是一个数组),以便用您需要的属性构造对象。您可以使用 map 作为主循环来返回每个迭代中构建的项目的新数组。要构建这些项目,您可以使用 forEach

var list = [snippet-code-js lang-js prettyprint-override] ['itemCode',1],['item','Pen'],['cashier','Sam']],[['itemCode',2],['item','Eraser] (function)(item(item)){obj [item [0]); ] = item [1];}); return obj;}); return newArray;} console.log(result(list));

I am looping to convert the array of array to object, but the final object has only the last item in the object. I am getting confused because you cant push in an object like array, and the number of loop is getting me frustrated. need help

here is the JSbin : http://jsbin.com/torawovicu/edit?js,console

Also how to get the object the same order as the array?

this is what the result should look like:

var newResult =
    [
        {itemcode: 1, item: 'Pen', 'cashier' : 'Sam'},
         {itemcode: 2, item: 'Eraser', 'cashier' : 'Kim'}
    ]

Here is my code

var list = [
    [
      ['itemCode', 1],
      ['item', 'Pen'],
      ['cashier', 'Sam']
    ],
    [
      ['itemCode', 2],
      ['item', 'Eraser'],
      ['cashier', 'Kim']
    ]
  ]
  //console.log(people.length);

function result(array) {
  var newObj = {};
  var newArr = [];
  for (var x in array) {
    //console.log(array[x])
    var item = array[x];
    for (var y in item) {
      var itemSingle = item[y]
        //console.log(itemSingle);
      for (i = 0; i < itemSingle.length; i = i + 2) {
        newObj[itemSingle[i]] = itemSingle[i + 1];
      }
    }
  }
  return newObj;
}
console.log(result(list));

解决方案

You have to use one loop to iterate over main array and then run loops to iterate over each array item (which also is an array) to construct object with properties you need. You can use map as main loop to return new array with items constructed inside each iteration. To construct those items you can use forEach:

var list = [
    [
      ['itemCode', 1],
      ['item', 'Pen'],
      ['cashier', 'Sam']
    ],
    [
      ['itemCode', 2],
      ['item', 'Eraser'],
      ['cashier', 'Kim']
    ]
  ];


function result(array) {
   let newArray = array.map(function(nestedArray) {
     let obj = {};
     nestedArray.forEach(function(item) {
       obj[item[0]] = item[1];
     });
     return obj;
     
   });
  return newArray;
}
console.log(result(list));

这篇关于数组到对象的数组 - javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆