1

I have problem.
I would like to create search array function, but in array I have objects and I don't know how referencing to keys this objects from function attribute.

var array= [{
    name: 'Joe',
    surname: 'Smith'
}, {
    name: 'John',
    surname: 'Smith'
}];


function searchAndGetIndex(arrayName, key, value) {
    var cloneArray = [...arrayName];
    var saveIndex = [];

    for(var i = 0; i < arrayName.length; i++) {
        var getIndex = cloneArray.findIndex(x => x.key == value); // x.key is my problem
        if(getFilterResult !== -1) saveIndex.push(getIndex + i);
        cloneArray.splice(getIndex , 1);
    }

    return saveIndex;
}

console.log(searchAndGetIndex(array, name, 'John')); // 1

Thanks for help.

3 Answers 3

1

IMHO, this is what you want:

var array= [{
    name: 'Joe',
    surname: 'Smith'
}, {
    name: 'John',
    surname: 'Smith'
}];

function searchAndGetIndex(arrayName, key, value) {
    let cloneArray = [...arrayName];
    let saveIndex = [];
    
    cloneArray.forEach((e, i) => {
      if(e[key] === value) {
        saveIndex.push(i)
      }
    })
    return saveIndex;
}

console.log(searchAndGetIndex(array, 'name', 'John'));

Sign up to request clarification or add additional context in comments.

Comments

0

Just do the below changes in your code and it will work:

  1. When calling searchAndGetIndex function, change name to 'name' because object keys are strings.

  2. Change x.key to x[key]

  3. Change getFilterResult to getIndex in if condition

Here is running code snippet:

var array= [{
    name: 'Joe',
    surname: 'Smith'
}, {
    name: 'John',
    surname: 'Smith'
}];


function searchAndGetIndex(arrayName, key, value) {
    var cloneArray = [...arrayName];
    var saveIndex = [];

    for(var i = 0; i < arrayName.length; i++) {
        var getIndex = cloneArray.findIndex(x => x[key] == value); // x.key is my problem
        if(getIndex !== -1) saveIndex.push(getIndex + i);
        cloneArray.splice(getIndex , 1);
    }

    return saveIndex;
}

console.log(searchAndGetIndex(array, 'name', 'John')); // 1

Comments

0

When you call searchAndGetIndex, name should be 'name', because name is a variable that does not exist. You are passing the string 'name', because object keys are strings.

var myObject = { test: 2 };
myObject[test];   // variable test is undefined
myObject.test;    // 2
myObject['test']; // 2

When you call findIndex, your x.key should be x[key], because x.key means to check the property named key on x, whereas x[key] means to check the property with the name that is the value of the variable key (in this case, the property named 'name').

var myObject = { key: 1, test: 2 };
var key = 'test';
myObject.key;     // 1
myObject['key'];  // 1
myObject[key];    // 2
myObject['test']; // 2

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.