I am trying to find the best way to filter my array of objects with specific key’s string. Basically what I am trying to achieve is to get the objects which contain "Type":"Blue"
. Here is my data:
[ { "data": [ {} ], "Name": "1", "Type": "Blue" }, { "data": [ {} ], "Name": "2", "Type": "Red" }, { "data": [ {} ], "Name": "3", "Type": "Blue" } ]
Advertisement
Answer
You could use the filter() method. See the snippet below, as well as a definition of the method from MDN:
The
filter()
method creates a new array with all elements that pass the test implemented by the provided function.
const data = [ { data: [{}], Name: "1", Type: "Blue" }, { data: [{}], Name: "2", Type: "Red" }, { data: [{}], Name: "3", Type: "Blue" } ]; const filteredData = data.filter((item) => item.Type === "Blue"); console.log(filteredData);