简体   繁体   English

将对象数组重新格式化为键控数组

[英]Reformat an array of objects to an array of key'd arrays

So I am kind of new to Javascript but I am trying to reformat the startArray into the endArray. 因此,我是Java语言的新手,但我尝试将startArray重新格式化为endArray。 The startArray is basically an array of order objects, which will always have different orderId's but can have the same companyId. startArray基本上是订单对象的数组,它们将始终具有不同的orderId,但可以具有相同的companyId。 I basically am trying to switch it so it's based on company, and so that for each companyId, there is an array of all of that companies orders. 我基本上是在尝试将其切换为基于公司的名称,因此对于每个companyId,所有这些公司订单都有一系列。 I have been tinkering and trying to figure this out, but to be honest I'm not sure where to start, or if this manipulation is even possible. 我一直在尝试并尝试解决这个问题,但是老实说,我不确定从哪里开始,甚至不确定这种操作是否可行。

I am working out of Google Apps Scripts, which I think is still on ES5 syntax, and I would prefer to stick to vanilla javascript if at all possible. 我正在研究Google Apps脚本,我认为它仍使用ES5语法,如果可能的话,我宁愿使用普通的javascript。

var startArray = [
    {"companyId" : 1,
     "orderId" : 25,
     "product" : "productA"
     "quantity" : 2,
     "price" : 10,
    },
    {"companyId" : 1,
     "orderId" : 20,
     "product" : "productB"
     "quantity" : 3,
     "price" : 5,
    },
   {"companyId" : 2,
     "orderId" : 15,
     "product" : "productA"
     "quantity" : 5,
     "price" : 10,
    }
 ]

 var endArray = [ {
      '1' = [{"orderId" : 25,
         "product" : "productA"
         "quantity" : 2,
         "price" : 10,},
         {"orderId" : 20,
          "product" : "productB"
          "quantity" : 3,
          "price" : 5,}
      },{
     '2' = [{"orderId" : 15,
          "product" : "productA"
          "quantity" : 5,
          "price" : 10,
         }]
   }]


 ]

If you are fine with result being object and not array, this should do the job: 如果您可以将结果作为对象而不是数组,则可以这样做:

 var startArray = [ {"companyId" : 1, "orderId" : 25, "product" : "productA", "quantity" : 2, "price" : 10, }, {"companyId" : 1, "orderId" : 20, "product" : "productB", "quantity" : 3, "price" : 5, }, {"companyId" : 2, "orderId" : 15, "product" : "productA", "quantity" : 5, "price" : 10, } ]; var ret = {} startArray.forEach(company => { if (!ret[company.companyId]) { ret[company.companyId] = []; } ret[company.companyId].push(company); }); console.log(ret); 

If you do not have ES6 support, just use classic for in cycle instead of forEach . 如果您不支持ES6,则只需for in循环中使用经典的for in而不是forEach

Use something like reduce to create a new object, inserting an array as the key of the companyId if it doesn't exist already: 使用诸如reduce方法创建一个新对象,如果还不存在,请插入一个数组作为companyId的键:

 function reduce(arr, callback, initialVal) { var accumulator = (initialVal === undefined) ? undefined : initialVal; for (var i = 0; i < arr.length; i++) { if (accumulator !== undefined) accumulator = callback.call(undefined, accumulator, arr[i], i, this); else accumulator = arr[i]; } return accumulator; } var startArray = [ {"companyId" : 1, "orderId" : 25, "product" : "productA", "quantity" : 2, "price" : 10, }, {"companyId" : 1, "orderId" : 20, "product" : "productB", "quantity" : 3, "price" : 5, }, {"companyId" : 2, "orderId" : 15, "product" : "productA", "quantity" : 5, "price" : 10, } ]; var endObj = reduce(startArray, function (accum, item) { var companyId = item.companyId; if (!accum[companyId]) accum[companyId] = []; delete item.companyId; accum[companyId].push(item); return accum; }, {}); console.log(endObj); 

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

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