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.

4
  • Does the object have to a complete match, or a partial match (either ways)? Commented Oct 9, 2022 at 12:01
  • @Terry Yes complete mtch Commented Oct 9, 2022 at 12:02
  • use JSON.stringify() to check for equality Commented Oct 9, 2022 at 12:07
  • @GrafiCode can you write sample code Commented Oct 9, 2022 at 12:09

3 Answers 3

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

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

5 Comments

What if the key/value pairs are in a different order?
@Andy would JSON.parse(JSON.stringify(obj)) solve it?
@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
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 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
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
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)) )

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.