Reputation: 27
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.
Upvotes: 0
Views: 40
Reputation: 324
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])
})
})
})
Upvotes: 1