从数组中查找对象索引

时间:2018-02-10 10:48:27

标签: javascript jquery arrays indexof

我正在尝试从数组

中找出所选对象索引

但它总是返回-1不知道为什么?

我正在尝试

我有以下数组,其中有多个对象

var data = [{
  "name": "abc",
  "age": 25,
  "school": "xyz pqr"
},
{
  "name": "abc1",
  "age": 26,
  "school": "xyz pqr"
},
{
  "name": "abc2",
  "age": 27,
  "school": "xyz pqr"
}]

这是我用户选择的另一个数组

var dList = [{
  "name": "abc",
  "age": 25,
  "school": "xyz pqr",
  "isChecked": true
}]

现在我想从数据数组中找出所选对象索引,并从该数组中删除该对象

if (dList.length > 0) {
  for (let i=0; i<dList.length; i++){
    delete dList[i]['isChecked']
    console.log(dList[i])
    console.log(data[0])
    console.log(dList[i] == data[0])
    let index = data.indexOf(dList[i]);
    console.log(index)
    data.splice(index, 1);
  }
}

7 个答案:

答案 0 :(得分:3)

这只是一个简单的实现:

if (dList.length > 0) {
  for (let i=0; i<dList.length; i++) {
    delete dList[i]['isChecked']
    console.log(dList[i])
    console.log(data[0])
    console.log(JSON.stringify(dList[i]) === JSON.stringify(data[0]))
    let index = data.findIndex(()=>dList[i]);
    console.log(index)
    data.splice(index, 1);
  }
}

比较对象可以通过使用JSON.stringify(ObjectName).

将其转换为字符串来完成

其次,而不是使用indexOf使用findIndexHereindexOffindIndex之间的主要区别。

答案 1 :(得分:2)

您只能比较两种基本类型,因此您无法通过比较来获取对象的索引。

您应该比较一些对于数组中每个对象唯一的主键。

&#13;
&#13;
var data = [{
  "name": "abc",
  "age": 25,
  "school": "xyz pqr"
},
{
  "name": "abc1",
  "age": 26,
  "school": "xyz pqr"
},
{
  "name": "abc2",
  "age": 27,
  "school": "xyz pqr"
}];

var index = data.findIndex(x => x.name=="abc2");

console.log(index); 
&#13;
&#13;
&#13;

答案 2 :(得分:1)

这将满足您的需求,一个更通用的版本,如果您有唯一ID,那将是最佳选择:

var data = [{
        "name": "abc",
        "age": 25,
        "school": "xyz pqr"
    },
    {
        "name": "abc1",
        "age": 26,
        "school": "xyz pqr"
    },
    {
        "name": "abc2",
        "age": 27,
        "school": "xyz pqr"
    }
];
var dList = [{
    "name": "abc",
    "age": 25,
    "school": "xyz pqr",
    "isChecked": true
}];


dList.forEach(function(obj) {
    delete obj.isChecked;
    data.splice(data.findIndex((o) => {
        return Object.getOwnPropertyNames(obj).every(p => obj[p] === o[p]);
    }), 1);
});

console.log(data);

另一种方式:

var data = [{
        "name": "abc",
        "age": 25,
        "school": "xyz pqr"
    },
    {
        "name": "abc1",
        "age": 26,
        "school": "xyz pqr"
    },
    {
        "name": "abc2",
        "age": 27,
        "school": "xyz pqr"
    }
];
var dList = [{
    "name": "abc",
    "age": 25,
    "school": "xyz pqr",
    "isChecked": true
}];


dList.forEach(function(obj) {
    delete obj.isChecked;
    data.splice(data.findIndex((o) => o.name === obj.name && o.age === obj.age && o.school === obj.school && o.school === obj.school), 1);
});

console.log(data);

不推荐的方式:

var data = [{
        "name": "abc",
        "age": 25,
        "school": "xyz pqr"
    },
    {
        "name": "abc1",
        "age": 26,
        "school": "xyz pqr"
    },
    {
        "name": "abc2",
        "age": 27,
        "school": "xyz pqr"
    }
];
var dList = [{
    "name": "abc",
    "age": 25,
    "school": "xyz pqr",
    "isChecked": true
}];


dList.forEach(function(obj) {
    delete obj.isChecked;
    data.splice(data.findIndex((o) => JSON.stringify(o) === JSON.stringify(obj)), 1);
});

console.log(data);

答案 3 :(得分:0)

您也可以使用

&#13;
&#13;
var data = [{
  "name": "abc",
  "age": 25,
  "school": "xyz pqr"
},
{
  "name": "abc1",
  "age": 26,
  "school": "xyz pqr"
},
{
  "name": "abc2",
  "age": 27,
  "school": "xyz pqr"
}];

var dList = [{
  "name": "abc",
  "age": 25,
  "school": "xyz pqr",
  "isChecked": true
}]

console.log(data.map(function(d){
return d.name;
}).indexOf(dList[0].name));
&#13;
&#13;
&#13;

答案 4 :(得分:0)

您无法比较两个对象表示法(JSON)。要比较两个JSON,首先需要对对象进行字符串化,然后JavaScript可以为您比较两个对象。

这是一个简单的代码,可以让你得到你想要的东西。

if (dList.length > 0) {
            for(var i=0; i<data.length; i++){
                for(var j=0; j<dList.length; j++){
                    delete dList[j]['isChecked'];
                    if(JSON.stringify(data[i]) === JSON.stringify(dList[j])){
                        let index = data.indexOf(data[i]);//Gets the index of the array
                        data.splice(index, 1);
                        console.log(data);

                    }else{
                        console.log('Data Not Matched in Array');
                    }
                }
            }
        }

答案 5 :(得分:0)

在某种意义上,没有通用的方法来确定一个对象与另一个对象是否相等。有关详细信息,请参阅 Equality comparisons

您可以找到并删除以下对象:

Array.prototype.remove = function(elem) {
  var indexElement = this.findIndex(el => el.name === elem.name);
  console.log(indexElement);
  if (indexElement != -1)
    this.splice(indexElement, 1);
  return this;
};

data.remove(dList[0]);
console.log(data);

Online demo (jsFiddle)

答案 6 :(得分:0)

var result= data.filter((item, i, self) => {
  if (item.name === 'abc2') {
    return { itemIndex: i, obj: item }
  }
});
var output = result.map(r => { console.log(r.itemIndex) })
console.log(output); 

这将返回名称为abc2的所有对象。 findIndex数组方法将始终返回1个可能不是这种情况的索引,因为人们可以使用相同的名称。

相关问题