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

124
Views
How to convert json object keys into different arrays removing the duplicate

I'm having the JSON like this i need to group this JSON with all the keys in JSON object and value should in array (excluding duplicates).

var people = [
    {sex:"Male", name:"Jeff"},
    {sex:"Female", name:"Megan"},
    {sex:"Male", name:"Taylor"},
    {sex:"Female", name:"Madison"}
];

My output should be like

{"sex":["Male","Female"],"name":["Jeff","Megan","Taylor","Madison"]}

how we can able to achieve this

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

0

function getValues(array) {
    var result = {};
    array.forEach(obj => {
        Object.keys(obj).forEach(key => { 
        if(!Array.isArray(result[key])) 
            result[key] = [];
        result[key].push(obj[key]);
        })
    })
return result;
}
about 4 years ago · Juan Pablo Isaza Report

0

You could use the Array.reduce() method to transform your array into a single object:

var people = [
    {sex:"Male", name:"Jeff"},
    {sex:"Female", name:"Megan"},
    {sex:"Male", name:"Taylor"},
    {sex:"Female", name:"Madison"}
];

const transformed = people.reduce((acc, e) => {
  Object.keys(e).forEach((k) => {
    if (!acc[k]) acc[k] = [];
    if (!acc[k].includes(e[k])) acc[k].push(e[k]);
  });
  return acc;
}, {});

console.log(transformed);

If for one of the object keys (sex or name in this case) a value array does not exist, it is created. Before a value is pushed into any of the value arrays, it is verified that it is not already present in that array.

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!