如何從對象數(shù)組中篩選符合條件的值
從對象數(shù)組中篩選符合條件的值
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)

將兩個對象數(shù)組根據(jù)相同的索引index合并為一個數(shù)組
this.currentTotalList = this.totalList.map((item, index) => ({ ...item, ...daysList[index] }))將兩個對象數(shù)組根據(jù)相同的鍵值合并為一個數(shù)組
let currentEveryList = this.everyList.map(item => ({...item, ...signList.filter(s => s.signDate === item.signDate)[0]}))從當(dāng)前數(shù)組中篩選符合條件的值
this.materialss = this.materials.filter(item => item.categoryId === this.curTab.categoryId)

js根據(jù)已有數(shù)組,從數(shù)組對象中篩選數(shù)據(jù)
例如,已得到以下源數(shù)據(jù)
? ? ? ? let dataArr = [
? ? ? ? ? ? { id: 1, age: 15 },
? ? ? ? ? ? { id: 2, age: 18 },
? ? ? ? ? ? { id: 3, age: 16 },
? ? ? ? ? ? { id: 4, age: 17 }
? ? ? ? ];現(xiàn)在需要跟據(jù)獲取的id數(shù)組(表格選中的行),篩選源數(shù)據(jù)
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>打印結(jié)果如下:

本來想用filter加forEach實現(xiàn)的,思路有點混亂爛尾了
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序?qū)崿F(xiàn)swiper切換卡內(nèi)嵌滾動條不顯示的方法示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)swiper切換卡內(nèi)嵌滾動條不顯示的方法,涉及微信小程序swiper選項卡組件相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
javascript實現(xiàn)dom動態(tài)創(chuàng)建省市縱向列表菜單的方法
這篇文章主要介紹了javascript實現(xiàn)dom動態(tài)創(chuàng)建省市縱向列表菜單的方法,可實現(xiàn)省市列表菜單效果,涉及javascript鼠標(biāo)事件及頁面處理json數(shù)據(jù)的技巧,需要的朋友可以參考下2015-05-05
javascript實現(xiàn)Email郵件顯示與刪除功能
這篇文章主要介紹了javascript實現(xiàn)Email郵件顯示與刪除功能,需要的朋友可以參考下2015-11-11

