将项目推送到对象键的值数组

时间:2017-09-22 13:11:55

标签: javascript arrays javascript-objects

我有对象数组:

[
 { pair_id: 1, exchange_pair_id: 183 },
 { pair_id: 1, exchange_pair_id: 2},
 ...
]

我想将此数组重建为

[
 { pair_id: 1, exchange_pair_id: [183, 2] },
 ...
]

这是我编写的代码(代码使我最接近所需的结果):

var array = [];

rows.forEach(function(row) {
    var obj = {};
    obj.pair_id = [row.pair_id];
    obj.exchange_pair_id = [row.exchange_pair_id]
    array.push(obj);
});

结果是:

[
 { pair_id: 1, exchange_pair_id: [183] },
 { pair_id: 1, exchange_pair_id: [2] },
 ...
]

这似乎是一个非常简单的问题,只有一个简单的解决方案,但我一直在绞尽脑汁,无法解决问题。

5 个答案:

答案 0 :(得分:4)

试试这个解决方案。我按Array#forEach迭代数组并尝试使用Array#find函数在groupedArray pair_id内找到一个元素。如果找到该元素,我将exchange_pair_id推入该元素的数组中。如果不是,我会根据项目的值在数组中创建一个新项目。



const array = [
  { pair_id: 1, exchange_pair_id: 183 },
  { pair_id: 1, exchange_pair_id: 2},
  { pair_id: 2, exchange_pair_id: 7},
  { pair_id: 3, exchange_pair_id: 988},
  { pair_id: 2, exchange_pair_id: 8},
  { pair_id: 3, exchange_pair_id: 98}
];

const groupedArray = [];

array.forEach(item => {
  
  const found = groupedArray.find(x => x.pair_id === item.pair_id);
  
  if(found) {
     found.exchange_pair_id.push(item.exchange_pair_id);
  } else {
     groupedArray.push({ pair_id: item.pair_id, exchange_pair_id: [item.exchange_pair_id]});
  }
  
});

console.log(groupedArray);




答案 1 :(得分:0)

试试这个:



var arr1 = [
    { pair_id: 1, exchange_pair_id: 183 },
    { pair_id: 1, exchange_pair_id: 2},
];

var arr2 = [];
arr1.forEach(el => {
    var obj = arr2.find(it => it.pair_id === el.pair_id);
    if (!obj) {
        el.exchange_pair_id = [el.exchange_pair_id];
        arr2.push(el);
    } else {
        obj.exchange_pair_id.push(el.exchange_pair_id);
    }
});

console.log(arr2);




答案 2 :(得分:0)

你很接近,你只需要检查是否有以前的结果。每次看到项目时,使用地图比使用find之类的内容查看列表更具时间效率。

var results = new Map();

rows.forEach(function(row) {
    previous_result = results.get(row.pair_id)
    if (previous_result != null) {
         previous_result.exchange_pair_id.push(row.exchange_pair_id);
    } else {
         var obj = {};
         obj.pair_id = row.pair_id;
         obj.exchange_pair_id = [row.exchange_pair_id]
         results.set(row.pair_id, obj);
    } 
});

Array.from(results.values())

答案 3 :(得分:0)

你要做的是聚合或缩减,所以我在这里使用Array.reduce()。依次获取每个元素并构建一组新元素,如下所示:

const rows = [
 { pair_id: 1, exchange_pair_id: 183 },
 { pair_id: 1, exchange_pair_id: 2},
 { pair_id: 4, exchange_pair_id: 6}
];

const result = rows.reduce( function( aggregate, nextElement ) {
   const pair_id = nextElement.pair_id;        
   
   //create a new row in the aggregate if one doesn't exist
   let aggregatedRow = aggregate.find( r => r.pair_id === pair_id );       
   if ( !aggregatedRow ) {
      aggregatedRow = { pair_id, exchange_pair_id: [] }; 
      aggregate.push( aggregatedRow );
   }
   //add the new exchange pair id to the aggregate row       
   aggregatedRow.exchange_pair_id.push( nextElement.exchange_pair_id );
       
   return aggregate;
       
}, []); //start with an empty array

console.log( result );

答案 4 :(得分:0)

您可以为相同的pair_id获取哈希表。

var array = [{ pair_id: 1, exchange_pair_id: 183 }, { pair_id: 1, exchange_pair_id: 2}],
    hash = Object.create(null),
    result = array.reduce(function (r, o) {
        if (!hash[o.pair_id]) {
            hash[o.pair_id] = [];
            r.push({ pair_id: o.pair_id, exchange_pair_id: hash[o.pair_id] });
        }
        hash[o.pair_id].push(o.exchange_pair_id);
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }