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

时间:2016-11-12 10:29:52

标签: javascript arrays object indexing

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

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

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

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

我需要通过它的完整内容(所有属性)找到对象的索引,而不仅仅是fieldgtelte。如何使用纯JavaScript在数组中找到对象的索引?

5 个答案:

答案 0 :(得分:2)

您可以迭代数据,然后检查密钥和每个密钥的长度(如果它具有相同的内容)。



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



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




答案 1 :(得分:2)

您可以使用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);




答案 2 :(得分:1)

正如我从您的进一步评论中所理解的那样,您只需要针对此特定案例的解决方案,其中对象由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);

如果您没有完整的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);

答案 3 :(得分:0)

您可以将此代码用于您的问题

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

答案 4 :(得分:0)

我使用比较对象的旧方法,通过 stringify 它。试着回顾这个

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