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

84
Views
Push object data into an array based on another object data value in javascript

I would like to push the values based on another value dynamically into an Array Here is my example:

const searchData = [
  {
    name: 'Name1',
    value: 'value1',
    class: 'class1',
  },
  {
    name: 'Name1',
    value: 'value2',
    class: 'class2',
  },
];

const data = [
  {
    ID: 'ID1',
    staff: [{ value1: 'hello1', value2: 'hallo1' }],
  },
  {
    ID: 'ID2',
    staff: [{ value1: 'hello2', value2: 'hallo2' }],
  },
];

const array = [];

data.foreach(dataElem => {
  dataElem.staff.foreach(staffElem => {
    searchData.foreach(searchElem => {
      array.push(staffElem[searchElem.value]); // all are undefined -> why?
    )}
  )}
})

I don't know why I getting undefined instead of the real values.

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

0

Your code has syntax error logic is right Compare this with above one

data.forEach(dataElem => {
  dataElem.staff.forEach(staffElem => {
    searchData.forEach(searchElem => {
      array.push(staffElem[searchElem.value])
    })
  })
})
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!