I have a list of items that resembles the following structure:
var entries = [
{ sys: {id:"1"}, fields: "article1" },
{ sys: {id:"2"}, fields: "place1" },
{ sys: {id:"3"}, fields: "offer2" },
{ sys: {id:"1"}, fields: "article2" },
{ sys: {id:"1"}, fields: "article3" },
{ sys: {id:"3"}, fields: "offer2" },
{ sys: {id:"2"}, fields: "place2" }
];
My goal is to divide this set into 3 separate arrays based on their sys.id
values. The desired output should be as follows:
var articles = [
{ sys: {id:"1"}, fields: "article1" },
{ sys: {id:"1"}, fields: "article2" },
{ sys: {id:"1"}, fields: "article3" }
];
var places = [
{ sys: {id:"2"}, fields: "place1" },
{ sys: {id:"2"}, fields: "place2" }
];
var offers = [
{ sys: {id:"3"}, fields: "offer2" },
{ sys: {id:"3"}, fields: "offer2" }
];
I've managed to achieve this by using a for loop, like so:
var places = [], offers = [], articles = [];
for (i=0; i<entries.length; i++) {
if (entries[i].sys.id === "1") results.articles.push(entries[i]);
else if (entries[i].sys.id === "2") results.places.push(entries[i]);
else if (entries[i].sys.id === "3") results.offers.push(entries[i]);
}
However, considering that the initial dataset will be larger and more complex, I wonder if there is a more efficient method than using a basic for loop. Is there a faster and more optimal approach?