Splitting an Array of Objects by Property

break array of objects into separate arrays based on a property

JQuery and Underscore are both options to use.

Underscore's groupBy does exactly what you need.

_.groupBy(arr, "type")

Split objects in array, based on the property

The easiest way would be to flatMap over the items, then map over the nominations.

const data = [{
id:1,
name: "ravi",
nominations: "xyz@gmail.com, abc@gmail.com"
},
{
id:2,
name: "ramu",
nominations: "123@gmail.com, 456@gmail.com"
}];

const result = data.flatMap(item => {
return item.nominations.split(", ").map(email => ({
id: item.id,
name: item.name,
nomination: email
}))
})

console.log(result)

In Javascript, is there a way to split an array into multiple arrays based on object property?

You can use Array.reduce to do this, we create an object with keys for each country.

We can then use Object.values to get this as an array.

const people = [
{ name: 'Tom Hiddleston', country: 'UK' },
{ name: 'Joe Biden', country: 'USA' },
{ name: 'James Bond', country: 'UK' },
{ name: 'Barack Obama', country: 'USA' },
{ name: 'Dolph Lundgren', country: 'SWEDEN' },
];

const grouped = Object.values(people.reduce((acc, item) => {
// Append the item to the array for each country
acc[item.country] = [...(acc[item.country] || []), item];
return acc;
}, {}))

console.log(grouped)

How to split an array of objects by arrays Javascript

You can simply do it using reduce:

const array = [{idx: 1, count: 100}, {idx: 2, count: 200}, {idx: 3, count: 200}, {idx: 4, count: 100}]
const result = array.reduce((carry, item) => { if (!carry.array.length || carry.count + item.count > 500) { carry.array.push([item]); carry.count = item.count; } else { carry.array[carry.array.length - 1].push(item); carry.count += item.count; } return carry;}, {array: [], count: 0}).array;
console.log(result);

split array of objects into three seperate array based on a property

This is an NP-complete problem so there is more then one solution. One approach I thought of is putting the bigger items first and then putting the smaller items where you have more space.

let items = [    {'id': '1', 'size': 10},    {'id': '2', 'size': 4},    {'id': '3', 'size': 6},    {'id': '4', 'size': 21},    {'id': '5', 'size': 2},    {'id': '6', 'size': 1},    {'id': '7', 'size': 6},    {'id': '8', 'size': 7},    {'id': '9', 'size': 8},    {'id': '1', 'size': 13}];//sort the array by size in reverse orderitems.sort((a, b)=> b.size - a.size);//array of sumslet sums = [0,0,0];//final array of 3 arrayslet arrays = [[],[],[]];for (let item of items) {    //get index of the smallest sum    let index =  sums.indexOf(Math.min.apply(null,sums));    //add current item size to corresponding sum    sums[index] += item.size;    //add item to corresponding array    arrays[index].push(item);}console.log(arrays);console.log(sums);

Split Array property from an Array of Objects into two separate Arrays

You can use ES6 array destructuring to do that for you. Instead of using Array.prototype.map, you should use Array.prototype.reduce because you can iterate through the entire myList once, and then store the x and y coordinates in separate sub-arrays:

const [x, y] = myList.reduce((accumulator, currentValue) => {
// Unpack/destructure nested arrays
const [x, y] = accumulator;

// Push coordinates into sub-arrays
x.push(currentValue.coords[0]);
y.push(currentValue.coords[1]);

// Repack and return updated accumulated values
return [x, y];
}, [[], []] as [number[], number[]]);

In the code above, you basically initialise the reduction process with an array that has the shape of a tuple containing array of numbers, i.e. [number[], number[]], where the first array stores your x coordinates and the second stores your y coordinates.

In each iteration of the reduce, you unpack/destructure the subarrays using const [x, y] = <currentValueReference>. Then, you simply push coords[0] (which is your x coordinate) and coords[1] (which is your y coordinate) into the unpacked arrays.

Then, you repack the arrays by simple returning [x, y].

See proof-of-concept example here.



Related Topics



Leave a reply



Submit