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

將兩個(gè)對(duì)象數(shù)組根據(jù)相同的索引index合并為一個(gè)數(shù)組
this.currentTotalList = this.totalList.map((item, index) => ({ ...item, ...daysList[index] }))將兩個(gè)對(duì)象數(shù)組根據(jù)相同的鍵值合并為一個(gè)數(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ù)組對(duì)象中篩選數(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實(shí)現(xiàn)的,思路有點(diǎn)混亂爛尾了
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于javascript實(shí)現(xiàn)的快速排序
本篇文章主要介紹了javascript實(shí)現(xiàn)的快速排序的方法與原理說明:找基準(zhǔn)點(diǎn)、建立二個(gè)數(shù)組分別存儲(chǔ)、遞歸。需要的朋友來看下吧2016-12-12
微信小程序?qū)崿F(xiàn)swiper切換卡內(nèi)嵌滾動(dòng)條不顯示的方法示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)swiper切換卡內(nèi)嵌滾動(dòng)條不顯示的方法,涉及微信小程序swiper選項(xiàng)卡組件相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
Javascript+CSS3實(shí)現(xiàn)進(jìn)度條效果
本篇文章主要介紹Javascript+CSS3實(shí)現(xiàn)進(jìn)度條效果,可以實(shí)現(xiàn)給用戶一個(gè)等待的過程,有需要的可以了解一下。2016-10-10
javascript實(shí)現(xiàn)簡(jiǎn)單的進(jìn)度條
本文給大家分享2個(gè)javascript實(shí)現(xiàn)簡(jiǎn)單的進(jìn)度條,一個(gè)是個(gè)人制作一個(gè)是網(wǎng)友實(shí)現(xiàn)的,都很不錯(cuò),這里推薦給大家。2015-07-07
微信小程序?qū)崿F(xiàn)左滑動(dòng)刪除效果
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)左滑動(dòng)刪除效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
javascript實(shí)現(xiàn)dom動(dòng)態(tài)創(chuàng)建省市縱向列表菜單的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)dom動(dòng)態(tài)創(chuàng)建省市縱向列表菜單的方法,可實(shí)現(xiàn)省市列表菜單效果,涉及javascript鼠標(biāo)事件及頁面處理json數(shù)據(jù)的技巧,需要的朋友可以參考下2015-05-05
javascript實(shí)現(xiàn)Email郵件顯示與刪除功能
這篇文章主要介紹了javascript實(shí)現(xiàn)Email郵件顯示與刪除功能,需要的朋友可以參考下2015-11-11

