Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
Convert array of objects into subarrays of objects by a selected property (javascript)

I am looking to take an array of objects, and convert it into an array of subarrays based on a chosen object property

for example:

[
  {foo: 1, bar: a}, {foo: 2, bar: b}, {foo: 1, bar: c}, {foo: 2, bar: d}
]

Create subarrays with the foo property would become:

[
  [
    {foo: 1, bar: a}, {foo: 1, bar: c}, 
  ],
  [
    {foo: 2, bar: b}, {foo: 2, bar: d}
  ]
]

I can't think of an efficient way to this. I keep resorting to creating a set of all of the unique property values of my chosen value, and then brute forcing through it multiple times.

const distinctProperties =  [...new Set(originalArray.map(item => item.foo))]

const newArray = distinctProperties.map(item => originalArray.filter(obj => obj.foo === item))

Thanks in advance for some advice here!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

can be done with reduce. you can wrap this around another function to group by the key you want.

Here I'm grouping based on the value of foo and res will be like this

{
  1: [{
  bar: "a",
  foo: 1
}, {
  bar: "c",
  foo: 1
}],
  2: [{
  bar: "b",
  foo: 2
}, {
  bar: "d",
  foo: 2
}]
}

then I'm taking Object.values of this to get the array format you want

let a =[
  {foo: 1, bar: 'a'}, {foo: 2, bar: 'b'}, {foo: 1, bar: 'c'}, {foo: 2, bar: 'd'}
]

let res= Object.values(a.reduce((acc,curr)=> {
    if(!acc[curr.foo])acc[curr.foo]=[];
  acc[curr.foo].push(curr)
    return acc;
},{}))

console.log(res)

As a generalized function to take key name as input

let a =[
  {foo: 1, bar: 'a'}, {foo: 2, bar: 'b'}, {foo: 1, bar: 'c'}, {foo: 2, bar: 'd'}
]


let groupByValue = (arr,key) => {
    return Object.values(arr.reduce((acc,curr)=> {
    if(!acc[curr[key]])acc[curr[key]]=[];
  acc[curr[key]].push(curr)
    return acc;
},{}))
}

let res1 = groupByValue(a,'foo')
let res2 = groupByValue(a,'bar')
console.log(res1)
console.log(res2)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!