JS6 - 如何循环遍历对象数组并在 country = UK 的位置进行过滤
JS6 - How can I loop through an array of objects and filter where country = UK
const arr =[
{
id:1,
name: 'Name',
country: 'UK',
},
{
id:2,
name: 'Name',
country: 'USA',
},
{
id:3,
name: 'Name',
country: 'USA',
},
{
id:4,
name: 'Name',
country: 'UK',
},
];
尝试使用 filter()
:
const arr =[ { id:1, name: 'Name', country: 'UK', }, { id:2, name: 'Name', country: 'USA', }, { id:3, name: 'Name', country: 'USA', }, { id:4, name: 'Name', country: 'UK', }, ];
const res = arr.filter(e => e.country === 'UK');
console.log(res);
使用filter
const arr =[
{
id:1,
name: 'Name',
country: 'UK',
},
{
id:2,
name: 'Name',
country: 'USA',
},
{
id:3,
name: 'Name',
country: 'USA',
},
{
id:4,
name: 'Name',
country: 'UK',
},
];
const uk = arr.filter(item => item.country === 'UK')
console.log(uk)
使用filter()
arr.filter(value => value.country === 'UK')
const arr =[
{
id:1,
name: 'Name',
country: 'UK',
},
{
id:2,
name: 'Name',
country: 'USA',
},
{
id:3,
name: 'Name',
country: 'USA',
},
{
id:4,
name: 'Name',
country: 'UK',
},
];
尝试使用 filter()
:
const arr =[ { id:1, name: 'Name', country: 'UK', }, { id:2, name: 'Name', country: 'USA', }, { id:3, name: 'Name', country: 'USA', }, { id:4, name: 'Name', country: 'UK', }, ];
const res = arr.filter(e => e.country === 'UK');
console.log(res);
使用filter
const arr =[
{
id:1,
name: 'Name',
country: 'UK',
},
{
id:2,
name: 'Name',
country: 'USA',
},
{
id:3,
name: 'Name',
country: 'USA',
},
{
id:4,
name: 'Name',
country: 'UK',
},
];
const uk = arr.filter(item => item.country === 'UK')
console.log(uk)
使用filter()
arr.filter(value => value.country === 'UK')