Referencing an object in an array using the function attribute

问题: 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. v...

问题:

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.


回答1:

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


回答2:

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'));


回答3:

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
  • 发表于 2018-07-08 10:04
  • 阅读 ( 276 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除