3

I have an array with multiple objects. I'd like to split this array to multiple arrays. The criteria to split is the item continent.

MyArray = [{continent:"europe", fruit:"orange", value:2},
           {continent:"asia", fruit:"banana", value:2},
           {continent:"europe", fruit:"apple", value:2},
           {continent:"asia", fruit:"apple", value:5}
          ];

Output:

[
 [{continent:"europe", fruit:"orange", value:2},
  {continent:"europe", fruit:"apple" value:2}
 ], [
  {continent:"asia", fruit:"banana", value:2},
  {continent:"asia", fruit:"apple" value:5}]
];

3 Answers 3

2

You could search for the array with the same continent and update this array or push a new array with the actual object.

var array = [{ continent: "europe", fruit: "orange", value: 2 }, { continent: "asia", fruit: "banana", value: 2 }, { continent: "europe", fruit: "apple", value: 2 }, { continent: "asia", fruit: "apple", value: 5 }],
    grouped = array.reduce(function (r, o) {
        var group = r.find(([{ continent }]) => continent === o.continent)
        if (group) {
            group.push(o);
        } else {
            r.push([o]);
        }
        return r;
    }, []);
    
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

0

Get the continent and use filter to get the matched elements

var MyArray = [{
  continent: "europe",
  fruit: "orange",
  value: 2
}, {
  continent: "asia",
  fruit: "banana",
  value: 2
}, {
  continent: "europe",
  fruit: "apple",
  value: 2
}, {
  continent: "asia",
  fruit: "apple",
  value: 5
}];
var getAllContinents = [];
 // getting the unique continent name
MyArray.forEach(function(item) {
  // if the continent  name is not present then push it in the array
  if (getAllContinents.indexOf(item.continent) === -1) {
    getAllContinents.push(item.continent)
  }
})
var modifiedArray = [];
//iterate over the continent array and find the matched elements where 
// continent name is same
getAllContinents.forEach(function(item) {
  modifiedArray.push(MyArray.filter(function(cont) {
    return cont.continent === item
  }))
})
console.log(modifiedArray)

Comments

0

You may also create an object with keys having values of specified property. This will allow you to access the desired array very easily.

You may use:

Example:

let data = [{continent:"europe", fruit:"orange", value:2},      
            {continent:"asia", fruit:"banana", value:2},
            {continent:"europe", fruit:"apple", value:2},
            {continent:"asia", fruit:"apple", value:5}];

let result = data.reduce((acc, obj) => {
  acc[obj.continent] = acc[obj.continent] || [];
  acc[obj.continent].push(obj);
  return acc;
}, {});

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.