目录从对象数组中筛选符合条件的值js根据已有数组,从数组对象中筛选数据解决思路从对象数组中筛选符合条件的值 const arr = [1,2,3,4,5,6,7] const lis
const arr = [1,2,3,4,5,6,7]
const list = [
{openId: 1, timelineId: 1, showNo: 1, uid: 1},
{openId: 2, timelineId: 1, showNo: 1, uid: 1},
{openId: 9, timelineId: 1, showNo: 1, uid: 1},
{openId: 4, timelineId: 1, showNo: 1, uid: 1},
{openId: 5, timelineId: 1, showNo: 1, uid: 1}
]
const params = list.filter(item=> arr.indexOf(item.openId) > -1)
console.log(params)
将两个对象数组根据相同的索引index合并为一个数组
this.currentTotalList = this.totalList.map((item, index) => ({ ...item, ...daysList[index] }))
将两个对象数组根据相同的键值合并为一个数组
let currentEveryList = this.everyList.map(item => ({...item, ...signList.filter(s => s.signDate === item.signDate)[0]}))
从当前数组中筛选符合条件的值
this.materialss = this.materials.filter(item => item.cateGoryId === this.curTab.categoryId)
例如,已得到以下源数据
let dataArr = [
{ id: 1, age: 15 },
{ id: 2, age: 18 },
{ id: 3, age: 16 },
{ id: 4, age: 17 }
];
现在需要跟据获取的id数组(表格选中的行),筛选源数据
let goalArr = [1, 2];
<script>
let dataArr = [
{ id: 1, age: 15 },
{ id: 2, age: 18 },
{ id: 3, age: 16 },
{ id: 4, age: 17 }
];
let goalArr = [1, 2];
let resArr = [];
goalArr.forEach((v, i) => {
dataArr.forEach((item, index) => {
if (item.id === v) {
resArr.push(item)
}
})
})
console.log(resArr)
//
</script>
打印结果如下:
本来想用filter加forEach实现的,思路有点混乱烂尾了
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: 如何从对象数组中筛选符合条件的值
本文链接: https://www.lsjlt.com/news/166548.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0