javascript array contains object
var obj = {a: 5};
var array = [obj, "string", 5]; // Must be same object
array.indexOf(obj) !== -1 // Truevendors.filter(function(vendor){ return vendor.Name === "Magenic" })// Works in all browsers
if (array.index(object) !== -1) {
console.log(`my object is in my array`)
}
// ES7 :
if(array.includes(oject)) {
console.log(`my object is in my array`)
}// To check if an array contains an Object
const myArrayObj = [{
'username': 'Player 1',
'email': '[email protected]'
}, {
'username': 'Player 2',
'email': '[email protected]'
}];
// Create a helper function to compear the objects
const isEqual = (first, second) => {
return JSON.stringify(first) === JSON.stringify(second);
}
const result = myArrayObj.some(e => isEqual(e, {
'username': 'Player 1',
'email': '[email protected]'
}));
console.log(result); // true
Also in JavaScript:
- async useeffect
- remove curly brackets from stringify javascript
- ./src/index.js Line 0: Parsing error: Cannot find module '@babel/helper-validator-identifier'
- vuejs pass all events to child
- get youtube embed code from url
- checking ascii in js
- see all functions in a website with console
- alpine.js install
- prime factorization javascript
- how to clone an object in javascript
- hackerrank fizzbuzz javascript
- passing json as datasource to jasper report library
- ckeditor ignore contenteditable
- javascript write to file and download
- javascript ascending and descending
- loading page for all ajax call in jquery 3.3.1
- eslint disable react
- python dictionary to json
- import a script to my react componetn
- google script get date without time
- how to reverse a string in javascript without using reverse method
- trailing comma javascript
- angular get firebase firestore
- javascript convert Object.entries back to object