简体   繁体   English

从数组中查找对象索引

[英]Find object index from array

I'm trying to find out selected object index from array 我正在尝试从数组中找出选定的对象索引

But it always return -1 don't know why? 但是它总是返回-1不知道为什么?

Here is I'm trying to do 这是我正在尝试做的

I have following array in which their are multiple objects 我有以下数组,其中它们是多个对象

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

And here is my another array that are selected by user 这是用户选择的另一个数组

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

Now I want to find out selected object index from data array and remove this object from that array 现在,我想从数据数组中找出选定的对象索引,并从该数组中删除该对象

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

Here is just a simple implementation: 这只是一个简单的实现:

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

Comparing the objects can be done by just converting it into string using JSON.stringify(ObjectName). 可以通过使用JSON.stringify(ObjectName).将对象转换为字符串来完成对象的比较JSON.stringify(ObjectName).

Second instead of using indexOf use findIndex . 第二,而不是使用indexOf使用findIndex Here is the main difference between indexOf and findIndex . indexOffindIndex之间的主要区别。

You can only compare two primitive types only so you will not be able to get the index of the object by comparing it. 您只能比较两个基本类型,因此您将无法通过比较来获取对象的索引。

You should instead compare some primary key which will be unique for each object inside the array. 相反,您应该比较一些主键,该主键对于数组内的每个对象都是唯一的。

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

this is going to meet your demand, a more universal version,if you got unique id,that is going to be the best choice: 这将满足您的需求,一个更通用的版本,如果您拥有唯一的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); 

another way: 其他方式:

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

unrecommended way: 不推荐的方式:

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

You can use this also 您也可以使用

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

You cannot compare two Object Notations(JSON). 您不能比较两个对象符号(JSON)。 To compare two JSONs you need to first stringify the object, then JavaScript can compare the two objects for you. 要比较两个JSON,您需要首先对对象进行字符串化,然后JavaScript可以为您比较两个对象。

Here is a simple code for you to get what you desire. 这是一个简单的代码,可让您获得想要的东西。

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

There is no generic means to determine that an object is equal to another in the sense. 从某种意义上说,没有通用的方法可以确定一个对象与另一个对象相等。 Please see Equality comparisons for more information. 请参阅平等比较以获取更多信息。

You can find and remove objects like below: 您可以找到和删除以下对象:

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) 在线演示(jsFiddle)

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

This will return all objects in which name is abc2 . 这将返回名称为abc2所有对象。 findIndex array method will always return 1 index that might not be the case as people can have the same name. findIndex数组方法将始终返回1个索引,但事实并非如此,因为人们可以使用相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM