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
Get array length and push into each object

I have a function

const output = {}
sites.forEach(obj=>{
    const ids = obj.ids
    if(ids && ids.length>0){
        ids.forEach(id=>{
            if(!output[id]){
                output[id] = []
            }
            output[id].push(obj.url);
        });
    }
});

fs.writeFileSync('cleanedData.json', JSON.stringify(output));

that takes data such as:

{ 
     url: "www.site.com", 
     ids: ["F20", "C10", "C05"] 
}, 
{ 
    url: "www.site.com/something", 
    ids: ["F20", "C05", "C10"] 
}, 
{ 
    url: "www.site.com/somethingelse", 
    ids: ["F20", "C12", "C05"] 
}

and transforms it so that I have a list of all ids and all urls with that id:

{
  "F20": [
    "www.site.com",
    "www.site.com/something",
    "www.site.com/somethingelse"
  ]
}, {
  "C10": [
    "www.site.com",
    "www.site.com/something"
  ]
}, {
  "C12": [
    "www.site.com/somethingelse"
  ]
}, {
  "C05": [
    "www.site.com",
    "www.site.com/something",
    "www.site.com/somethingelse"
  ]
}

I'm able to get a count of each id's array length with

console.log(Object.values(output).map(id => id.length))

But can't figure out how to include that as part of the forEach, so that I have a key/value in each object that shows the length of the array.

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

0

Use nested objects in the output, and give it a length property that you increment when you push onto the array.

const sites = [{
    url: "www.site.com",
    ids: ["F20", "C10", "C05"]
  },
  {
    url: "www.site.com/something",
    ids: ["F20", "C05", "C10"]
  },
  {
    url: "www.site.com/somethingelse",
    ids: ["F20", "C12", "C05"]
  }
];

const output = {};
sites.forEach(obj => {
  const ids = obj.ids
  if (ids && ids.length > 0) {
    ids.forEach(id => {
      if (!output[id]) {
        output[id] = {length: 0, urls: []}
      }
      output[id].urls.push(obj.url);
      output[id].length++;
    });
  }
});

console.log(output);

about 4 years ago · Juan Pablo Isaza Report

0

You can use Array.prototype.reduce() combined with Array.prototype.forEach():

const sites = [{url: "www.site.com",ids: ["F20", "C10", "C05"]},{url: "www.site.com/something",ids: ["F20", "C05", "C10"]},{url: "www.site.com/somethingelse",ids: ["F20", "C12", "C05"]}]

const output = sites.reduce((a, { url, ids }) => {
  ids.forEach(id => {
    a[id] = a[id] || { length: 0, urls: [] }
    a[id].length += 1
    a[id].urls.push(url)
  })
  return a
}, {})

console.log(output)

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!