Skip to content Skip to sidebar Skip to footer

Check If An Array "includes" An Item In Javascript

I am trying to understand the array 'includes' function. My goal is to determine if an array includes a certain item. It works fine for an array of strings but when using objects i

Solution 1:

You need to use Array.prototype.some() in this case. Array.prototype.includes() does not accept a function parameter*, and you're not testing for strict equality.

const itemsObject = [{ name: "name1" }, { name: "name2" }];

console.log(itemsObject.some(x => x.name === "name1"));

*Peer pressure from the comments section forces me to clarify that includes() does accept function parameters, but will not use the passed function as a predicate to determine whether a given item matches. Rather, it will try to find an item in the array that is strictly equal to the passed function.

Solution 2:

In your last line you check wether a function is inside of your array. .includes also works for objects, but it compares them by reference. In your case you probably want to .find or check wether .some of the objects match your query.

Solution 3:

Does "includes" work with objects or do I need to use another function?

Includes works with objects, but it compares objects by reference. In your case, despite the first element of itemsObject has the same keys and values of itemToSearch, they are different objects, hence includes will not work, since, for objects cases, it looks for the same object instance.

In order to make it work, you can use several alternatives, like .find, .some, .filter.

Another solution, which I personally don't recommend but I think that it's worth mentioning, is that you can use .includes if you first map items to strings instead. In that case, using JSON.stringify, you can check whether the objects are the same. BEWARE: this will work with single key items only. JSON.stringify doesn't preserve key and values order, so it works with single keys objects only (unless keys and values are in the same order in the original stringified object). Moreover, the JSON.stringify way is way heavier and less performant than the others, I just think it's worth mentioning that as an example.

Below some examples with each of them.

var itemsString = ["name1", "name2"];
    var itemsObject = [{ name: "name1" }, { name: "name2" }];
    var itemToSearch = { name: "name1" };
    
    console.log(itemsObject.some(r => r.name === itemToSearch.name));
    console.log(!!itemsObject.find(r => r.name === itemToSearch.name));
    //          ^--- !! is used to cast to boolean.console.log(itemsObject.filter(r => r.name === itemToSearch.name).length > 0);
    console.log(itemsObject.map(i =>JSON.stringify(i)).includes(JSON.stringify(itemToSearch)));
    //          ^--------------^                       ^---------------------------------------^//            |------ this will stringify each object, converting it to a json string.  |//                                                                                      |//                this will check whether the string[] includes any stringified value.--^

Post a Comment for "Check If An Array "includes" An Item In Javascript"