Below is my JSON Array. I need to get the objects from the array which have objects with key
:name and value
:cricket.
Is there any way I can achieve this without using loops?
[
{
"name": "cricket",
"ground": "JBL Ground",
"capacity": "50000"
},
{
"name": "rugby",
"ground": "IPL Ground",
"capacity": "55000"
},
{
"name": "running",
"ground": "PPL Ground",
"capacity": "10000"
},
{
"name": "cricket",
"ground": "MBL Ground",
"capacity": "34000"
},
{
"name": "cricket",
"ground": "KIG Ground",
"capacity": "19000"
}
]
Using Array.filter
const allData = [
{
"name": "cricket",
"ground": "JBL Ground",
"capacity": "50000"
},
{
"name": "rugby",
"ground": "IPL Ground",
"capacity": "55000"
},
{
"name": "running",
"ground": "PPL Ground",
"capacity": "10000"
},
{
"name": "cricket",
"ground": "MBL Ground",
"capacity": "34000"
},
{
"name": "cricket",
"ground": "KIG Ground",
"capacity": "19000"
}
]
const wantedData = allData.filter(item => item.name === 'cricket');
console.log(wantedData);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.