How to Determine If JavaScript Array Contains an Object With an Attribute That Equals a Given Value

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

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

You've done all the hard work, just change the signature of your function to return a bool instead of the index, and return true or false:

func isExists(key string, value string, data []map[string]interface{}) (exists bool) {

for _, search := range data {
if search[key] == value {
return true
}
}
return false
}

https://play.golang.org/p/lJGwa6pZaX5

How to determine if Javascript array contains an object with an attribute that is equal to each other

You could take Array#every and check the first item to each other. The method stops the iteration if the condition is false and returns then false, if not then true.

The callback's parameter can have three parts, one for the item, the next for the index and the third one has the reference to the array.

In this case only the item is used with a destructuring of LRN and the array. The unneeded index is denoted by an underscore, which is a valid variable name in Javascript.

if (!array.every(({ LRN }, _, a) => LRN === a[0].LRN)) {
this.openSnackBar ("LRN mismatching");
}

Code with example of all property LRN having the same value.

const
selectedData = [{ Name: 'Michelle', LRN: '100011' }, { Name: 'Micheal', LRN: '100011'}, { Name: 'Mick', LRN: '100011' }];

if (!selectedData.every(({ LRN }, _, a) => LRN === a[0].LRN)) {
console.log("LRN mismatching");
} else {
// just to show
console.log('OK');
}

Check if an array of object is exactly equal to another array that contains one of its property

as @mplungjan mentiond, you can use Every:

let fixed = ["123", "456", "789"];
let variableArray1 = [{
name: "Joe",
id: "123"
}, {
name: "Joe",
id: "456"
}, {
name: "Joe",
id: "789"
}];
let variableArray2 = [{
name: "Joe",
id: "123"
}, {
name: "Joe",
id: "456"
}, {
name: "Joe",
id: "001"
}]

let containsAll1 = variableArray1.every(elem => fixed.includes(elem.id));
let containsAll2 = variableArray2.every(elem => fixed.includes(elem.id));

console.log(containsAll1, containsAll2);

Array.includes() to find object in array

Array.includes compares by object identity just like obj === obj2, so sadly this doesn't work unless the two items are references to the same object. You can often use Array.prototype.some() instead which takes a function:

const arr = [{a: 'b'}]

console.log(arr.some(item => item.a === 'b'))

Check if sub object of javascript object contains a certain string

You are almost there:

const included = things.some(thing => thing.party.name === "Bob")

You need to provide a function to some. That function will be called for every element and should return something that (when coerced to a boolean) indicates whether the element matches your criteria or not. In this case we pass a function that returns true if an element's party property has a name property that is equal to "Bob".

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 array of object contains a string

Since you need to check the object property value in the array, you can try with Array​.prototype​.some():

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

let arr = [

{

name: 'Jack',

id: 1

},

{

name: 'Gabriel',

id: 2

},

{

name: 'John',

id: 3

}

]

var r = arr.some(i => i.name.includes('Jack'));

console.log(r);


Related Topics



Leave a reply



Submit