0

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.

1
  • The function is forEach()... check your console for errors. Commented Feb 8, 2022 at 11:27

1 Answer 1

1

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])
    })
  })
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.