javascript数组对象的筛选使⽤filter,find⽅法通过筛选条件筛选数组对象中的元素
let arr = [
{code: '1', name: '苹果'},
{code: '2', name: '雪梨'},
{code: '3', name: '葡萄'},
{code: '4', name: '⾹蕉'},
{code: '5', name: '橙⼦'},
]
let searchCode = '3'
let searchByFilter = arr.filter(item => de === searchCode)
let searchByFind = arr.find(item => de === searchCode)
filter过滤对象数组
console.log(searchTarget) // [{code: "3", name: "葡萄"}],查不到则返回 []
console.log(searchByFind) // {code: "3", name: "葡萄"}, 查不到则返回undefined
两个数组对象取共同的集合
let arr2 = [
{code: '1', name: '苹果'},
{code: '3', name: '葡萄'},
{code: '6', name: '西⽠'},
{code: '7', name: '荔枝'}
]
let conmonArr = arr.filter(item => arr2.some(it => it.code === de))
console.log(conmonArr)
/
/ [
// {code: "1", name: "苹果"},
// {code: "3", name: "葡萄"}
//]
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论