2

I just started to learn javascript today and i'm trying to check an array object to check if the array does NOT contain a object.

For an example i am trying to check if data2 does not contain data1.

let data1 = [{ x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }];
let data2 = [
 { x: 6352, y: 10656, type: "data1", dead: 0, uid: 3832861 },
 { x: 6322, y: 10546, type: "data2", dead: 0, uid: 3832862 },
 { x: 6542, y: 15356, type: "data3", dead: 0, uid: 3832863 }
];
let check = Object.entries(data2).find(([key, value]) => value.uid != data1[0].uid);
console.log(check); // Expected output to be fales.

If someone could help me or point me in the right direction i would really appreciate it.

asked Aug 3, 2021 at 15:54
4
  • Is there a unique property in your objects? This is often why objects have an id. Commented Aug 3, 2021 at 15:57
  • @LaytonGB I assume that is the uid property. Commented Aug 3, 2021 at 15:57
  • 3
    Does this answer your question? How to determine if object is in array Commented Aug 3, 2021 at 15:57
  • 1
    What would be the criteria to consider an item in data1 equal to an item in data2? Should the both have the exact same key/value pairs? It suffices they have the same uid? Something else? Commented Aug 3, 2021 at 15:59

4 Answers 4

2

You need to compare (===) the result of find with !undefined.

Edit: You do not need to call entries on an array.

const data1 = [{ x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }];
const data2 = [
 { x: 6352, y: 10656, type: "data1", dead: 0, uid: 3832861 },
 { x: 6322, y: 10546, type: "data2", dead: 0, uid: 3832862 },
 { x: 6542, y: 15356, type: "data3", dead: 0, uid: 3832863 }
];
const check = data2.find(value => value.uid === data1[0].uid) !== undefined;
console.log(check); // false

As others have noted, you can short-circuit with some. Here is an optimized version:

/**
 * Detrmines if an object exists within an array of object
 * @param {Object[]} arr - An array of object to search
 * @param {Object} obj - An object to search for
 * @param {Function} fn - A pluck function used for comparing
 * @return {Boolean} Returns true if the item exists
 */
const contains = (arr, obj, fn) =>
 (res => arr.some(e => fn(e) === res)) // Compare 
 (fn(obj)); // Pre-calculate as `res`
 
const data1 = [{ x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }];
const data2 = [
 { x: 6352, y: 10656, type: "data1", dead: 0, uid: 3832861 },
 { x: 6322, y: 10546, type: "data2", dead: 0, uid: 3832862 },
 { x: 6542, y: 15356, type: "data3", dead: 0, uid: 3832863 }
];
const [obj] = data1; // Destructure first item
console.log(contains(data2, obj, ({ uid }) => uid)); // false
console.log(contains(data2, { uid: 3832862 }, ({ uid }) => uid)); // true

answered Aug 3, 2021 at 15:57

Comments

1

The way you are tackling this problem is okay, and the other answer will help you fix it. But this is ideally supposed to be solved as an array. With Object.entries() you are getting the array objects and indices as values & keys respectively. Then you iterate over them.

You can notice you are not even using the key variable.

No need to do all this since you already have an array.

You can use .some(), which returns true/false based on a condition which even one of the array item fulfills. Exactly what you need.

let data1 = [{ x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }];
let data2 = [
 { x: 6352, y: 10656, type: "data1", dead: 0, uid: 3832861 },
 { x: 6322, y: 10546, type: "data2", dead: 0, uid: 3832862 },
 { x: 6542, y: 15356, type: "data3", dead: 0, uid: 3832863 }
];
let check = data2.some(x => x.uid === data1[0].uid);
console.log(check); // Expected output to be fales.

answered Aug 3, 2021 at 16:12

Comments

1

You have a few syntax errors in your code above. data2 is an array of objects so you don't need to do Object.entries() to get the elements of the array. I would use Array.some() method here so that it returns a boolean value instead of the actual data element if you are hoping to have a true or false value logged. the Array.some() method just returns once one case is true and we don't have to go through all the entries in the array of object if we already found a match.

let data1 = [{ x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }];
let data2 = [
 { x: 6352, y: 10656, type: "data1", dead: 0, uid: 3832861 },
 { x: 6322, y: 10546, type: "data2", dead: 0, uid: 3832862 },
 { x: 6542, y: 15356, type: "data3", dead: 0, uid: 3832863 },
 { x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }
];
let check = data2.some(entry => entry.uid == data1[0].uid);
console.log(check); // Expected output to be true.

If you didn't have a unique id for each entry then a quick and dirty way to do this would be to stringify both objects and then compare the strings. This has its limitations because the order of the fields in your object must be the same.

var object = { 'a': 1, 'b':2};
var other = { 'a': 1, 'b':2 };
 
console.log(JSON.stringify(object) == JSON.stringify(other)); //this will print true

For deep object comparison, I like to use the isequal method from lodash

var object = { 'a': 1, 'b':2};
var other = { 'b':2, 'a': 1 };
 
console.log(JSON.stringify(object) == JSON.stringify(other)); //this will print false
console.log(_.isEqual(object, other)); //this will print true

answered Aug 3, 2021 at 16:22

Comments

1

Solution with Object.entries() (the exact approach you've taken with minimum changes):

let data1 = [{ x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }];
let data2 = [
 { x: 6352, y: 10656, type: "data1", dead: 0, uid: 3832861 },
 { x: 6322, y: 10546, type: "data2", dead: 0, uid: 3832862 },
 { x: 6542, y: 15356, type: "data3", dead: 0, uid: 3832863 }
];
let check;
Object.entries(data2).find(([key, value]) => check = value.uid === data1[0].uid);
console.log(check); //false

answered Aug 3, 2021 at 16:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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