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

187
Views
group by in array using nested array's object field value javascript

I have an array like this

data = [
  { name : "sa", attributes : [{skin : "green"},{nose:"good"}]},
  { name : "sa", attributes : [{skin : "red"},{nose:"bad"}]},
  { name : "sa", attributes : [{skin : "green"},{nose:"good"}]},
  { name : "sa", attributes : [{nose:"good}]},
]

so i want the grouping of array based on skin attribute. I am using _ underscore library like this

const attributeType = "skin";
const groupedCollections = _.groupBy(colls, (col) => {
  const data = col.attributes.find((attribute) => {
    const keys = Object.keys(attribute);
    return keys.indexOf(attributeType) > -1 ? true : false
  });
  return data?.value
});

but this is making grouping under undefined.

Any help?

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

0

You need to return the value of the wanted property.

const
    colls = [{ name: "sa", attributes: [{ skin: "green" }, { nose: "good" }] }, { name: "sa", attributes: [{ skin: "red" }, { nose: "bad" }] }, { name: "sa", attributes: [{ skin: "green" }, { nose: "good" }] }, { name: "sa", attributes: [{ nose: "good" }] }],
    attributeType = "skin",
    groupedCollections = _.groupBy(colls, ({ attributes }) =>  attributes
        .find(attribute => attributeType in attribute)
        ?.[attributeType]
    );

console.log(groupedCollections);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

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!