How to find all the data of object by given id. For example this is my array of obbject:
const array = [{
"name": "max",
"id": 10
},
{
"name": "ram",
"id": 20
},
{
"name": "sita",
"id": 30
},
{
"name": "bill",
"id": 10
},
]
Now i wanna create function that returns all data that has id
of 10. The function should return, {"name": "max", "id": 10}
and {"name": "bill", "id": 10}
.
I tried doing this:
const array = [{
"name": "max",
"id": 10
},
{
"name": "ram",
"id": 20
},
{
"name": "sita",
"id": 30
},
{
"name": "bill",
"id": 10
},
]
function findData(data, id) {
const found = data.find(element => element.id === id)
return found
}
console.log(findData(array, 10))
but the problem is, it just returns one object instead of returning all the data