I have array
const arr = [
{id: 1, country: 'Austria'},
{id: 2, country: 'Germany'},
{id: 3, country: 'Austria'},
];
I tried the following the code
arry.map((item,idx) => (
item.orderstatuszerodha== "done"?(
console.log(item.id)
)
:
null
))
output is 1 3 but I want to get output after whole filter like 1,3
I want only the id where country=Austria
like this
1,3
CodePudding user response:
use reduce
function
const arry = [
{id: 1, country: 'Austria'},
{id: 2, country: 'Germany'},
{id: 3, country: 'Austria'},
];
const result = arry.reduce((acc,item,idx) => {
if(item.country == "Austria"){
if(idx === 0) acc = item.id
else acc = `,${item.id}`
}
return acc
}, '')
console.log(result)
CodePudding user response:
This is How I Filter
const arry = [
{id: 1, country: 'Austria'},
{id: 2, country: 'Germany'},
{id: 3, country: 'Austria'},
];
const result = arry.filter((item, index) => {
if(item.country == "Austria"){
return true
}
else{
return false
}
})
console.log(result)
CodePudding user response:
One line answer:
arr.filter(i => i.country == 'Austria')