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

117
Views
Converting javascript array data into an object

convert this type of data

const data = [
  { name: 'hcs_utils', starCount: 0 },
  { name: 'K', starCount: 0 },
  { name: 'Heroes of Wesnoth', starCount: 0 },
  { name: 'Leiningen', starCount: 1 },
  { name: 'TearDownWalls', starCount: 1 }]

into this kind of object {"K":5, "Leiningen":4, ...}

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

0

const data = [
  { name: "hcs_utils", starCount: 0 },
  { name: "K", starCount: 0 },
  { name: "Heroes of Wesnoth", starCount: 0 },
  { name: "Leiningen", starCount: 1 },
  { name: "TearDownWalls", starCount: 1 },
];

/* 
    // desired format
    {
        "hcs_utils": 0,
        "K": 0,
        // so on 
    }

*/

const formattedDataArray = data.map((each) => {
  const { name, starCount } = each;
  return {
    [name.toString()]: starCount,
  };
});

console.log("formattedDataArray is", formattedDataArray);

// with reduce

const resultWithReduce = formattedDataArray.reduce((prev, current) => {
  return {
    ...prev,
    ...current,
  };
}, {});

console.log("resultWithReduce is", resultWithReduce);

// without reduce

let resutlWithoutReduce = {};

formattedDataArray.forEach((eachElement) => {
  const key = Object.keys(eachElement)[0];
  const value = Object.values(eachElement)[0];
  resutlWithoutReduce[key] = value;
});

console.log("resultWithoutReduce is", resutlWithoutReduce);

Hope it helps!

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!