I am trying to loop through a array ob objects and group the items of the array into new arrays that have matching id:
API example:
api_array [
var objs = [
{ id: 1, postcode: "xxx", street: "xxx", city: "xxx" },
{ id: 1, postcode: "xxx", street: "xxx", city: "xxx" },
{ id: 2, postcode: "xxx", street: "xxx", city: "xxx" },
{ id: 3, postcode: "xxx", street: "xxx", city: "xxx" }
];
var result = objs.reduce(function(r, a) {
r[a.id] = r[a.id] || [];
r[a.id].push(a);
return r;
}, Object.create(null));
console.log(result);
//Create a javascript array of objects containing key value pairs id, post
var api_array = [
{id: 1, postcode: '10'},
{id: 1, postcode: '11'},
{id: 2, postcode: '20'},
{id: 2, postcode: '21'},
{id: 2, postcode: '22'},
{id: 3, postcode: '30'}
];
//result is a javascript array containing the groups grouped by id.
let result = [];
//javascript array has a method foreach that enumerates keyvalue pairs.
api_array.forEach(
r => {
//if an array index by the value of id is not found, instantiate it.
if( !result[r.id] ){
//result gets a new index of the value at id.
result[r.id] = [];
}
//push that whole object from api_array into that list
result[r.id].push(r);
}
);
console.log(result[1]);
console.log(result[2]);
console.log(result[3]);
Prints:
[ { id: 1, postcode: '10' }, { id: 1, postcode: '11' } ]
[ { id: 2, postcode: '20' },
{ id: 2, postcode: '21' },
{ id: 2, postcode: '22' } ]
[ { id: 3, postcode: '30' } ]
You could use the ids as properties of an object
let api_array = [
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
];
let grouped = groupArray(api_array);
console.log(grouped);
console.log(grouped[1]);
function groupArray(myArray) {
let grouped = {};
for (let i = 0; i < myArray.length; i++) {
let row = myArray[i];
let group = grouped[row.id];
if (!group) {
group = [];
grouped[row.id] = group;
}
group.push(row);
}
return grouped;
}
api_array [
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
];
let result = [];
for (let i = 0; i < numberOfGroupsIWantToMake; i++) {
let newGroupArray = api_array.filter(obj => obj.id === i);
result.push(newGroupArray);
}
return result;
Note: this is A solution, but it's not as performant as we're going over the entire array n number of times.
Try to acheive like this:
var api_array = [
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
];
const result = api_array.reduce((acc, item) => {
acc[`group_${item.id}`] = (acc[`group_${item.id}`] || []);
acc[`group_${item.id}`].push(item);
return acc;
}, {});
console.log(result);
Note: The result will have keys group_1
, group_2
... instead group_one
, group_two
...
If you strictly need that, then make an array for key and values to convert 1 as one.
https://jsfiddle.net/u4k16ojz/5/
var result = new Array(4);
api_array.forEach(function(item, index){
if (!result[item.id]){
result[item.id] = [];
}
result[item.id].push(item);
})