I have an array of object,I need to get those objects only whose property "name" having similar value.I have provided the expected output also.Here is the code below.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<script>
const value =
[
{
"id": 2,
"name": "Pumpkin"
},
{
"id": 1,
"name": "Ladies Finger"
},
{
"id": 4,
"name": "Spinach"
},
{
"id": 5,
"name": "Spinach"
}
]
console.log("get same value property")
/*Expected output
[
{
"id": 4,
"name": "Spinach"
},
{
"id": 5,
"name": "Spinach"
}
]
*/
</script>
</body>
</html>
3
Answers
You can iterate over array once to create the key value pairs to store the occurence of the elements. And then use this key value pair to filter out the array elements using filter function. Something like this
Good question!
https://jsfiddle.net/coder039/3fu87h6j/1/