I have an array of post that is call test and the next code takes other array and push the post if the name equals something:
var test = [];
docs = [{name: 'mateo',lastName: 'mateo'},
{name: 'Jhon',lastName: 'smith'}}]
docs.forEach(function(item) {
if(item.name === 'mateo'){
test.push(item);
}
});
I want to use different parameters for the name, but I don´t know how, I was trying to use something like this but it did´t work:
var test = [];
docs = [{name: 'mateo', lastName: 'mateo'},
{name: 'Jhon',lastName: 'smith'}}]
const varNames = ['name','lastName'];
docs.forEach(function(item) {
for(i = 0; i < varNames.length; i++){
if(item.[varNames[i]] === 'mateo'){
test.push(item);
console.log(varNames[i]);
}
}
});
item.["varNames[i]"]withitem[varNames[i]]testalready have elements in it beforeforEachis called?