2

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?

6
  • 1
    what is unique among all these objects? Commented Nov 12, 2016 at 10:32
  • 1
    Can your object have any properties that are objects/arrays themselves? (nested) Commented Nov 12, 2016 at 10:46
  • 2
    findIndex! Commented Nov 12, 2016 at 11:07
  • @user2181397 The combination of field, gteand lte. And gte and lte can be different from each other. Commented Nov 12, 2016 at 11:47
  • @Bergi findIndex doesn't work on the whole object, and the uniqueness of the objects are the combination of object property values. Commented Nov 13, 2016 at 11:51

5 Answers 5

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

What is the reason of using Array.prototype.some just for iteration? (instead of Array.prototype.forEach)
it ends the iteration if the first matched object is found. forEach iterates all items of the array.
@NinaScholz you probably meant to return true inside the if if you want some to end early. The current code will return the last matching index.
And it's working really well! Great answer. I'm using the ES6 version.
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.

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

Comments

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:

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:

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

Comments

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

Comments

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

2 Comments

this works only if the properties have the same order, which is not guarantied with objects.
ya that's the main drawback. exactly agree with you @NinaScholz

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.