Javascript:在数组中查找对象的索引,而不知道索引,只知道对象

时间:2021-03-17 04:15:14

I have an array of objects and I want to remove one of the objects.

我有一个对象数组,我想删除其中一个对象。

[
  {"field":"ingredients","gte":"egg","lte":"egg"},
  {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"},
  {"field":"ingredients","gte":"hvetemel","lte":"hvetemel"}
]

I don't know the index of the object I want to remove, but I know the whole object. I.e:

我不知道要删除的对象的索引,但我知道整个对象。即:

{"field":"ingredients","gte":"bakepulver","lte":"bakepulver"}

I need to find the object's index by it's full content (all properties), not just by field, gte or lte. How do I find object's index in the array with just plain JavaScript?

我需要通过对象的完整内容(所有属性)来查找对象的索引,而不仅仅是字段、gte或lte。如何用普通JavaScript在数组中找到对象的索引?

5 个解决方案

#1


2  

You could iterate over the data and then check the length of the keys and every key, if it has the same content.

您可以对数据进行迭代,然后检查键和每个键的长度,如果它们有相同的内容。

var data = [{ "field": "ingredients", "gte": "egg", "lte": "egg" }, { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }, { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }],
    search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" },
    keys = Object.keys(search),
    index = -1;

data.some(function (a, i) {
    if (Object.keys(a).length === keys.length && keys.every(function (k) { return a[k] === search[k]; })) {
        index = i;
        return true;
    }
});

console.log(index);

ES6

ES6

var data = [{ "field": "ingredients", "gte": "egg", "lte": "egg" }, { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }, { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }],
    search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" },
    keys = Object.keys(search),
    index = data.findIndex(a =>
        Object.keys(a).length === keys.length && keys.every(k => a[k] === search[k]));

console.log(index);

#2


2  

You can use Object.keys(), Array.prototype.findIndex(), Array.prototype.every() to check if each property name, value, and object property names .length are equal.

可以使用object .keys()、Array.prototype.findIndex()、Array.prototype.every()检查每个属性名、值和对象属性名.length是否相等。

let data = [
  {"field":"ingredients","gte":"egg","lte":"egg"},
  {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"},
  {"field":"ingredients","gte":"hvetemel","lte":"hvetemel"}
];

let props = {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"};
let keys = Object.keys(props);
let index = data.findIndex(o => keys.every(key => o[key] === props[key]) 
              && Object.keys(o).length === keys.length);

console.log(index);

#3


1  

As I understand from your further comments, you need a solution for this specific case only, where the objects consist of 3 given properties. Therefore I would suggest this ES6 solution:

正如我从您的进一步评论中了解到的,您只需要针对这个特定的情况提供一个解决方案,其中对象由3个给定的属性组成。因此,我建议这个ES6解决方案:

var data = [
  { "field": "ingredients", "gte": "egg", "lte": "egg" },
  { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" },
  { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }];
var search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" };

var index = data.findIndex(
    a => a.field == search.field && a.gte == search.gte && a.lte == search.lte);

console.log(index);

If you don't have full ES6 support, then:

如果你没有充分的ES6支持,那么:

var data = [
  { "field": "ingredients", "gte": "egg", "lte": "egg" },
  { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" },
  { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }];
var search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" };

var index = -1;
data.some(function (a, i) {
    if (a.field == search.field && a.gte == search.gte && a.lte == search.lte)
        return index = i, true;
});
console.log(index);

#4


0  

You could use this code for your problem

你可以用这个代码来解决你的问题。

var array = [
    {"field":"ingredients","gte":"egg","lte":"egg"},
    {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"},
    {"field":"ingredients","gte":"hvetemel","lte":"hvetemel"}
];
var searchObject =      
    {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"};

var indexOfSearchResult;

for (var i = 0; i < array.length; i++) {
    var checkEqualBool = true;
    for (var key in array[i]) {
        if(array[i][key]!=searchObject[key]){
            checkEqualBool = false;
            break;
        }
    }
    if(checkEqualBool){
        indexOfSearchResult = i;
        break;
    }
}

#5


0  

I use old method of comparing object, by stringify it. try to review this one

我用旧的比较对象的方法,通过字符串化。试着复习一下这个

var x = [{
  "field": "ingredients",
  "gte": "egg",
  "lte": "egg"
}, {
  "field": "ingredients",
  "gte": "bakepulver",
  "lte": "bakepulver"
}, {
  "field": "ingredients",
  "gte": "hvetemel",
  "lte": "hvetemel"
}];
var control = {
  "field": "ingredients",
  "gte": "bakepulver",
  "lte": "bakepulver"
};

function getIndex(arr, key) {
  var got = false,
    result = -1;
  arr.every(function(e, i) {
    if (JSON.stringify(e) === JSON.stringify(key)) {
      console.log('match found');
      result = i;
      return false;
    }
    return true;
  })
  return result;
}

console.log(getIndex(x, control));

#1


2  

You could iterate over the data and then check the length of the keys and every key, if it has the same content.

您可以对数据进行迭代,然后检查键和每个键的长度,如果它们有相同的内容。

var data = [{ "field": "ingredients", "gte": "egg", "lte": "egg" }, { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }, { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }],
    search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" },
    keys = Object.keys(search),
    index = -1;

data.some(function (a, i) {
    if (Object.keys(a).length === keys.length && keys.every(function (k) { return a[k] === search[k]; })) {
        index = i;
        return true;
    }
});

console.log(index);

ES6

ES6

var data = [{ "field": "ingredients", "gte": "egg", "lte": "egg" }, { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }, { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }],
    search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" },
    keys = Object.keys(search),
    index = data.findIndex(a =>
        Object.keys(a).length === keys.length && keys.every(k => a[k] === search[k]));

console.log(index);

#2


2  

You can use Object.keys(), Array.prototype.findIndex(), Array.prototype.every() to check if each property name, value, and object property names .length are equal.

可以使用object .keys()、Array.prototype.findIndex()、Array.prototype.every()检查每个属性名、值和对象属性名.length是否相等。

let data = [
  {"field":"ingredients","gte":"egg","lte":"egg"},
  {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"},
  {"field":"ingredients","gte":"hvetemel","lte":"hvetemel"}
];

let props = {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"};
let keys = Object.keys(props);
let index = data.findIndex(o => keys.every(key => o[key] === props[key]) 
              && Object.keys(o).length === keys.length);

console.log(index);

#3


1  

As I understand from your further comments, you need a solution for this specific case only, where the objects consist of 3 given properties. Therefore I would suggest this ES6 solution:

正如我从您的进一步评论中了解到的,您只需要针对这个特定的情况提供一个解决方案,其中对象由3个给定的属性组成。因此,我建议这个ES6解决方案:

var data = [
  { "field": "ingredients", "gte": "egg", "lte": "egg" },
  { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" },
  { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }];
var search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" };

var index = data.findIndex(
    a => a.field == search.field && a.gte == search.gte && a.lte == search.lte);

console.log(index);

If you don't have full ES6 support, then:

如果你没有充分的ES6支持,那么:

var data = [
  { "field": "ingredients", "gte": "egg", "lte": "egg" },
  { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" },
  { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }];
var search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" };

var index = -1;
data.some(function (a, i) {
    if (a.field == search.field && a.gte == search.gte && a.lte == search.lte)
        return index = i, true;
});
console.log(index);

#4


0  

You could use this code for your problem

你可以用这个代码来解决你的问题。

var array = [
    {"field":"ingredients","gte":"egg","lte":"egg"},
    {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"},
    {"field":"ingredients","gte":"hvetemel","lte":"hvetemel"}
];
var searchObject =      
    {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"};

var indexOfSearchResult;

for (var i = 0; i < array.length; i++) {
    var checkEqualBool = true;
    for (var key in array[i]) {
        if(array[i][key]!=searchObject[key]){
            checkEqualBool = false;
            break;
        }
    }
    if(checkEqualBool){
        indexOfSearchResult = i;
        break;
    }
}

#5


0  

I use old method of comparing object, by stringify it. try to review this one

我用旧的比较对象的方法,通过字符串化。试着复习一下这个

var x = [{
  "field": "ingredients",
  "gte": "egg",
  "lte": "egg"
}, {
  "field": "ingredients",
  "gte": "bakepulver",
  "lte": "bakepulver"
}, {
  "field": "ingredients",
  "gte": "hvetemel",
  "lte": "hvetemel"
}];
var control = {
  "field": "ingredients",
  "gte": "bakepulver",
  "lte": "bakepulver"
};

function getIndex(arr, key) {
  var got = false,
    result = -1;
  arr.every(function(e, i) {
    if (JSON.stringify(e) === JSON.stringify(key)) {
      console.log('match found');
      result = i;
      return false;
    }
    return true;
  })
  return result;
}

console.log(getIndex(x, control));