JavaScript處理數(shù)組數(shù)據(jù)的示例詳解
數(shù)據(jù)
[ { title: '市值', prop: 'sz', titleData: [ { title: '市值', label: '市值', prop: 'sz', isShow: false, fixed: false, width: 100, align: 'left' }, { title: '持倉(cāng)/市值', label: '持倉(cāng)/市值', prop: 'ccsz', isShow: false, fixed: false, width: 120, align: 'left' } ] }, { title: '持倉(cāng)', prop: 'cc', titleData: [ { title: '資金費(fèi)率', label: '資金費(fèi)率', prop: 'avgFundingRateByOi', isShow: false, fixed: false, width: 100, align: 'left' }, { title: '持倉(cāng)', label: '持倉(cāng)', prop: 'openInterest', isShow: false, fixed: false, width: 100, align: 'left' } ] } ]
循環(huán)上面數(shù)組 并把titleData中的每一項(xiàng)和下面這個(gè)數(shù)組中每一項(xiàng)進(jìn)行對(duì)比,單prop相等時(shí),將下面的匹配項(xiàng)覆蓋到上面對(duì)應(yīng)的位置
[{ title: '持倉(cāng)', label: '持倉(cāng)', prop: 'openInterest', fixed: false, width: 100, isShow: true, align: 'left' }, { title: '持倉(cāng)變化(24h)', label: '持倉(cāng)(24h%)', prop: 'h24OiChangePercent', fixed: false, width: 100, isShow: true, align: 'left' }, { title: '多(4小時(shí))', label: '多(4h)', prop: 'h4LongVolUsd', fixed: false, width: 100, isShow: true, align: 'left' }]
實(shí)現(xiàn)
data.forEach(item => { item.titleData.forEach(titleItem => { const match = newData.find(newItem => newItem.prop === titleItem.prop); if (match) { Object.assign(titleItem, match); } }); });
會(huì)遍歷data數(shù)組中的每個(gè)對(duì)象,然后對(duì)每個(gè)對(duì)象的titleData數(shù)組進(jìn)行遍歷。在遍歷titleData數(shù)組的過(guò)程中,會(huì)查找與newData數(shù)組中具有相同prop屬性的對(duì)象。如果找到匹配項(xiàng),則使用Object.assign方法將匹配項(xiàng)的屬性覆蓋到titleData數(shù)組中的相應(yīng)對(duì)象上。
最終,data數(shù)組中的titleData數(shù)組將被更新為匹配項(xiàng)的屬性值。
const data = [ { label: "收藏", prop: "sc", fixed: true, width: 60, isShow: true, align: "center" }, { label: "排名", prop: "pm", fixed: true, width: 60, isShow: true, align: "center" }, { label: "幣種", prop: "symbol", fixed: true, width: 90, isShow: true, align: "left" }, { label: "價(jià)格", prop: "price", fixed: false, width: 100, isShow: true, align: "left" }, { title: "價(jià)格變化(24h)", label: "價(jià)格(24h%)", prop: "h24PriceChangePercent", fixed: false, width: 100, isShow: true, align: "left" } ];
循環(huán)上面數(shù)組 把下面的數(shù)字和上面匹配prop,當(dāng)上面數(shù)組存在下面的某一項(xiàng)時(shí),將其isshow字段賦值為下面的,如果isshow為false時(shí),將從上面數(shù)組中刪除,如果不存在則追加到上面數(shù)組中
const newData = [ { title: '持倉(cāng)', label: '持倉(cāng)', prop: 'openInterest', fixed: false, width: 100, isShow: true, align: 'left' }, { title: '持倉(cāng)變化(24h)', label: '持倉(cāng)(24h%)', prop: 'h24OiChangePercent', fixed: false, width: 100, isShow: false, align: 'left' }, { title: '多(4小時(shí))', label: '多(4h)', prop: 'h4LongVolUsd', fixed: false, width: 100, isShow: true, align: 'left' } ];
使用
newData.forEach(newItem => { const matchIndex = data.findIndex(item => item.prop === newItem.prop); if (matchIndex !== -1) { if (newItem.isShow) { data[matchIndex].isShow = true; } else { data.splice(matchIndex, 1); } } else { data.push(newItem); } }); console.log(data);
到此這篇關(guān)于JavaScript處理數(shù)組數(shù)據(jù)的示例詳解的文章就介紹到這了,更多相關(guān)JavaScript處理數(shù)組數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript 點(diǎn)擊觸發(fā)復(fù)制功能實(shí)例詳解
這篇文章主要介紹了JavaScript 點(diǎn)擊觸發(fā)復(fù)制功能實(shí)例詳解,需要的朋友可以參考下2018-11-11跟我學(xué)習(xí)javascript的基本類型和引用類型
跟我學(xué)習(xí)javascript的基本類型和引用類型,利用圖解的方式,告訴大家javascript的基本類型和引用類型的區(qū)別和聯(lián)系,感興趣的小伙伴們可以參考一下2015-11-11JavaScript實(shí)現(xiàn)excel文件導(dǎo)入導(dǎo)出
這篇文章主要介紹了JavaScript實(shí)現(xiàn)excel文件導(dǎo)入導(dǎo)出,文件的導(dǎo)入導(dǎo)出是非常常見(jiàn)的需求功能,excel文件的導(dǎo)入導(dǎo)出更為常見(jiàn),實(shí)踐中許多時(shí)候,是調(diào)用接口實(shí)現(xiàn)導(dǎo)入導(dǎo)出的,具體實(shí)現(xiàn)內(nèi)容需要的小伙伴可以參考一下2022-06-06JS如何實(shí)現(xiàn)form表單登錄驗(yàn)證并使用MD5加密詳解
表單驗(yàn)證為終端用戶檢測(cè)無(wú)效的數(shù)據(jù)并標(biāo)記這些錯(cuò)誤,是一種用戶體驗(yàn)的優(yōu)化,下面這篇文章主要給大家介紹了關(guān)于JS如何實(shí)現(xiàn)form表單登錄驗(yàn)證并使用MD5加密的相關(guān)資料,需要的朋友可以參考下2023-06-06javascript 函數(shù)參數(shù)限制說(shuō)明
我依稀記得哪本書上有說(shuō)過(guò),實(shí)參數(shù)限制是32個(gè)? 現(xiàn)在想想估計(jì)是我記錯(cuò)了..他也許說(shuō)的是32位.2010-11-11