2

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]);
        }
     } 
});
4
  • Please update your post to include a complete, runnable example. Commented Oct 14, 2018 at 23:25
  • Replace item.["varNames[i]"] with item[varNames[i]] Commented Oct 14, 2018 at 23:26
  • If i use [varNames[i]] i get a syntax error when running nodejs ( I get an error with ["..."] too, I don´t know how to make that work is the problem. Commented Oct 14, 2018 at 23:27
  • @ManuelEsteban does test already have elements in it before forEach is called? Commented Oct 14, 2018 at 23:41

3 Answers 3

3

That's not how bracket notation work. Change

if(item.["varNames[i]"] === 'mateo') {

To:

if(item[varNames[i]] === 'mateo') {

By the way, you should break the loop once an item is matched:

if(item[varNames[i]] === 'mateo') {
    test.push(item);
    break;
}

You could also replace that whole for loop with another array function called some, so that your whole code looks like:

docs.forEach(function(item) {
    var check = varNames.some(function(varName) {
        return item[varName] === 'mateo';
    });
    if(check) {
        test.push(item);
    }
})

Which can be shorter if you use an arrow function:

docs.forEach(item => {
    if(varNames.some(varName => item[varName] === 'mateo')) {
        test.push(item);
    }
})
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you the some() method for arrays is the solution to my problem.
@ManuelEsteban You're welcome! BTW, the for loop works fine too, you just need to correct the syntax of the bracket notation and add the break statement.
2

You should really be using filter() for this, everything will be easier:

let docs = [
  {name: 'mateo', lastName: 'johnson'},
  {name: 'Jhon',lastName: 'smith'},
  {name: 'Mark',lastName: 'mateo'}
]

var test = docs.filter(item =>item.name === "mateo") 
console.log(test)

Then to check for an array of names you can simple use some() to see if some of the properties match:

let docs = [
  {name: 'mateo', lastName: 'johnson'},
  {name: 'Jhon',lastName: 'smith'},
  {name: 'Mark',lastName: 'mateo'}
]
const varNames = ['name','lastName'];

var test = docs.filter(item => varNames.some(name => item[name] == 'mateo'))

console.log(test)

Comments

0

The trick is to iterate over both varNames and docs. You're familiar with .forEach, so this example uses it, though this may be a better case for .filter.

const test = [];
const varNames = ['name', 'otherName'];
const docs = [
  { name: 'mateo' },
  { name: 'name' },
  { name: 'otherName' }
];

varNames.forEach(varName => {
  docs.forEach(doc => {
    if (doc.name === varName) test.push(doc);
  });
});
console.log(test);

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.