JSON array has

JSON array has

In the following example, we will filter the objects whose color property has 'red' in it.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <body> <h1>JSON array has</h1> <p>Filter all person who has 'red' in their array (color).</p> <button onclick="myFunction()">Click Me</button> <script> var data = [ { "name": "Danny", "age": 25, "color": ["blue", "red", "green"] }, { "name": "Mike", "age": 35, "color": ["black", "red", "purple"] }, { "name": "Jade", "age": 18, "color": ["yellow", "black", "green"] } ] var data1 = []; function myFunction(){ for(var i = 0; i < data.length; i++){ if(data[i].color.indexOf("red") > -1) data1.push(data[i]); } console.log(data1); } </script> </body> </html>

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.

Share this Page

Meet the Author