How to check if a deep array value is present

Active3 hr before
Viewed126 times

9 Answers


There must be a terser way to identify whether a given array value I'm referencing exists? It seems far too verbose to have to test for the presence of both the value I seek, and all its ancestry as well. In some circumstances that makes sense, yes, but not all., Are there any gaps in the range of gravitational wave frequencies we can detect? ,By example: it might represent, say, user->session->cart, where I want a way to quickly check whether the cart has entries, without having to include a check each for whether the user exists, then whether the session exists, then whether the cart exists, then ..., 3 Just to clarify, this also safely tests the existence of $a: it returns false if $a is not set. – Jonathan Lidbeck Sep 7 '15 at 0:40

The following will work as you expect:

if (isset($a['a']['b']['c']))

You can use the in operator to loop over an object to check if a specific property key exists. (Knowledge shared by @fvbix),Here's a Code Recipe to check if a #JavaScript array contains a value. You can use the new array includes method 😋 For older browsers and IE, you can use indexOf 👍,Stack Overflow: How do I check if an array includes a value in JavaScript? , How to Check if Object is Empty in JavaScript

const array = ['🥗', '🍔', '🍰'];

// Modern Browser
array.includes('🍰'); // true

// Older Browser
array.indexOf('🍰') !== -1; // true
load more v

Checking if an array contains an object is slightly more complex than searching for primitive values.,A primitive value in JavaScript is a string, number, boolean, symbol, and special value undefined.,What other ways to detect if an array contains a value do you know?,In this post, you will read about how to determine if an array contains a particular value, being a primitive or object.

The easiest way to determine if an array contains a primitive value is to use array.includes() ES2015 array method:

javascriptconst hasValue = array.includes(value[, fromIndex]);
load more v

var arr = [{
      id: 1,
      name: 'JOHN'
      id: 2,
      name: 'JENNIE'
      id: 3,
      name: 'JENNAH'

function userExists(name) {
   return arr.some(function(el) {
      return === name;

console.log(userExists('JOHN')); // true
console.log(userExists('JUMBO')); // false
load more v

array_key_exists — Checks if the given key or index exists in the array, An array with keys to check. , array_key_exists() returns true if the given key is set in the array. key can be any value possible for an array index. ,in_array() - Checks if a value exists in an array

load more v

You can use the JavaScript some() method to find out if a JavaScript array contains an object.,This method tests whether at least one element in the array passes the test implemented by the provided function. Here's an example that demonstrates how it works:,How to check if a value exists in an array in JavaScript,How to check if an object property is undefined in JavaScript

// An array of objects
var persons = [{name: "Harry"}, {name: "Alice"}, {name: "Peter"}];

// Find if the array contains an object by comparing the property value
if(persons.some(person => === "Peter")){
    alert("Object found inside the array.");
} else{
    alert("Object not found.");

Asserts that the target is deeply equal to the given obj. See the deep-eql project page for info on the deep equality algorithm:,Add .deep earlier in the chain to use deep equality instead. See the deep-eql project page for info on the deep equality algorithm:,By default, strict (===) equality is used. Add .deep earlier in the chain to use deep equality instead. See the deep-eql project page for info on the deep equality algorithm:,By default, members are compared using strict (===) equality. Add .deep earlier in the chain to use deep equality instead. See the deep-eql project page for info on the deep equality algorithm:

expect(function() {}).to.not.throw();
   a: 1
expect([1, 2])'array').that.does.not.include(3);
load more v

Arrays have an includes method that checks whether a given value exists in the array. The function uses that to determine whether the event name it is interested in is part of the event list for a given day.,The Object.keys function will be useful when you need to go over the properties of objects to compare them.,The concat method can be used to glue arrays together to create a new array, similar to what the + operator does for strings.,The push method adds values to the end of an array, and the pop method does the opposite, removing the last value in the array and returning it.

To run over a list (in listToArray and nth), a for loop specification like this can be used:

for (let node = list; node; node = {}

You can also update values using an index in the same way.,I just scratched the surface on arrays but that should put them into the right context as I move onto hashtables.,This also accepts wildcards so you can set values in bulk. Here are some ways you can use that:,In most cases, you can just test for the value with something like this:

Before I jump into what a Hashtable is, I need to mention arrays first. For the purpose of this discussion, an array is a list or collection of values or objects.

$array = @(1, 2, 3, 5, 7, 11)
load more v

Other "value-check" queries related to "How to check if a deep array value is present"