How to check if an Array Includes an object in JavaScript?
Answer
Use JavaScript Array some() method to check whether an array includes an object or not.
This method returns a Boolean value by testing whether at least one element in the array passes the test implemented by the provided function.
Example
HTML Online Compiler
// Object
var players = [{
"name": "Alex",
"age": 26
}, {
"name": "Batista"'
"age": 60
}, {
"name": "Corey",
"age": 39
}];
// JavaScript some() method to check if an Array Includes an object in JavaScript.
if (players.some(player => player.name === "Corey")) {
alert("Included.");
} else {
alert("Not Included.");
}
Related Tutorial
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.