0

I have an array of objects like this:

[
    {"firstKey":"firstValue","secondKey":"secondValue"},
    {"thirdKey":"thirdValue","forthKey":"forthValue"}
]

and object like

{"thirdKey":"thirdValue","forthKey":"forthValue"}

I want to check if this object exists in an array of objects or no
NOTE: The keys of objects are generated dynamically and I don't know the name of the keys.

paranoid
  • 6,799
  • 19
  • 49
  • 86

3 Answers3

1

You can use JSON.stringify() to compare objects:

const arr = [
    {"firstKey":"firstValue","secondKey":"secondValue"},
    {"thirdKey":"thirdValue","forthKey":"forthValue"}
]

const needle = {"thirdKey":"thirdValue","forthKey":"forthValue"}

console.log( arr.filter(o => JSON.stringify(o) === JSON.stringify(needle)) )
GrafiCode
  • 3,307
  • 3
  • 26
  • 31
1

SOLUTION: (only works when two the keys are in same order)

Use find() and JSON.stringify()

const arr = [
    {"firstKey":"firstValue","secondKey":"secondValue"},
    {"thirdKey":"thirdValue","forthKey":"forthValue"}
];

const look = {"thirdKey":"thirdValue","forthKey":"forthValue"}
const look2 = {"thirdKey":"fourthValue","forthKey":"forthValue"}

let exists1 = arr.find((x) => JSON.stringify(x) === JSON.stringify(look)) === undefined ? false : true ;

let exists2 = arr.find((x) => JSON.stringify(x) === JSON.stringify(look2)) === undefined ? false : true ;

console.log(exists1);
console.log(exists2);

EDIT:

JSON.stringify(x) === JSON.stringify(y) will only work when strings are in same order. Otherwise you will have to use a complex function to compare the two objects. Check

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
  • 1
    What if the key/value pairs are in a different order? – Andy Oct 09 '22 at 12:18
  • @Andy would `JSON.parse(JSON.stringify(obj))` solve it? – GrafiCode Oct 09 '22 at 12:20
  • @Andy thanks for pointing that out. That is a separate question in itself. Either we use some library or write a different function to compare two JS strings. I don't think parsing will solve it – Tushar Shahi Oct 09 '22 at 12:26
  • it's on MDN: «For example, JSON.stringify on the same object will always produce the same string, and JSON.parse(JSON.stringify(obj)) would produce an object with the same key ordering as the original (assuming the object is completely JSON-serializable).» I'm wondering if this is fully reliable https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify – GrafiCode Oct 09 '22 at 12:31
  • 1
    **As original** right @GrafiCode but what if we have `{a : 1, b : 2}` and `{b : 2 ,a :1}`. These JSON strings when parsed will convert into objects where key for each respective object will have the same order as their string version. Yet, their order will be different and converting back to string again will not lead them to be equal – Tushar Shahi Oct 09 '22 at 12:34
  • thanks for the explaination, I guess lodash wins – GrafiCode Oct 09 '22 at 12:38
0

What you need to do is get the keys of the object, using Object.keys(object), and for every object in the array do the same, than compare them, as follows:

function findEqual(object, array) {
    var objectKeys = Object.keys(object);
    for (var i = 0; i < array.length; i++) {
        var currObjectKeys = Object.keys(array[i]);
        if (currObjectKeys.length !== objectKeys.length) {
            continue;
        }
        var found = true;

        for (var j = 0; j < objectKeys.length; j++) {
            if (objectKeys[j] !== currObjectKeys[j]) {
                found = false;
                break;
            }
            if (object[objectKeys[j]] !== array[i][objectKeys[j]]) {
                found = false;
                break;
            }
        }
        if (found) {
            return i;
        }
    }
    
    return -1;
}
Osmar
  • 198
  • 9