How to Determine If Object Is in Array

How to determine if Javascript array contains an object with an attribute that equals a given value?

2018 edit: This answer is from 2011, before browsers had widely supported array filtering methods and arrow functions. Have a look at CAFxX's answer.

There is no "magic" way to check for something in an array without a loop. Even if you use some function, the function itself will use a loop. What you can do is break out of the loop as soon as you find what you're looking for to minimize computational time.

var found = false;
for(var i = 0; i < vendors.length; i++) {
if (vendors[i].Name == 'Magenic') {
found = true;
break;
}
}

Check if object value exists within a Javascript array of objects and if not add a new object to array

I've assumed that ids are meant to be unique here. some is a great function for checking the existence of things in arrays:

const arr = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }];
function add(arr, name) { const { length } = arr; const id = length + 1; const found = arr.some(el => el.username === name); if (!found) arr.push({ id, username: name }); return arr;}
console.log(add(arr, 'ted'));

Check if object is in array of objects

You can check if the object is present in the array using the Array.some() method (ref).

const is_present = eligiblePCVoucher.some((e) => {
return Object.entries(e).toString() === Object.entries(addressObj).toString();
});

What this will do is concatenate the key and value of each of the properties in an object and do an exact string comparison. Some() will return true only if at least an object matches

How to check if variable is an object or an array?

This would help.

var a = [], b = {};
console.log(Object.prototype.toString.call(a).indexOf("Array")>-1);console.log(Object.prototype.toString.call(b).indexOf("Object")>-1);
console.log(a.constructor.name == "Array");console.log(b.constructor.name == "Object");

Javascript check if object by Id is in array

let id = 3;
const arr = [{
id: 1,
name: 'James'
}, {
id: 2,
name: 'Peter'
}, {
id: 3,
name: 'Mike'
}]

var chek = arr.find(c => c.id === id);

console.log(chek ?? "Not present")
//or:
if (chek === undefined) {
console.log("Not present")
} else {
console.log(chek.name)
}

How to check if object is an array of a certain type?

Use Type.IsArray and Type.GetElementType() to check the element type of an array.

Type valueType = value.GetType();
if (valueType.IsArray && expectedType.IsAssignableFrom(valueType.GetElementType())
{
...
}

Beware the Type.IsAssignableFrom(). If you want to check the type for an exact match you should check for equality (typeA == typeB). If you want to check if a given type is the type itself or a subclass (or an interface) then you should use Type.IsAssignableFrom():

typeof(BaseClass).IsAssignableFrom(typeof(ExpectedSubclass))

How can I check if an object is an array?

In modern browsers you can do:

Array.isArray(obj)

(Supported by Chrome 5, Firefox 4.0, Internet Explorer 9, Opera 10.5 and Safari 5)

For backward compatibility you can add the following:

// Only implement if no native implementation is available
if (typeof Array.isArray === 'undefined') {
Array.isArray = function(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
};

If you use jQuery you can use jQuery.isArray(obj) or $.isArray(obj). If you use Underscore.js you can use _.isArray(obj).

If you don't need to detect arrays created in different frames you can also just use instanceof:

obj instanceof Array


Related Topics



Leave a reply



Submit