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簡(jiǎn)單實(shí)現(xiàn)合并兩個(gè)Json對(duì)象的方法示例
這篇文章主要介紹了JavaScript簡(jiǎn)單實(shí)現(xiàn)合并兩個(gè)Json對(duì)象的方法,結(jié)合實(shí)例形式分析了json對(duì)象的遍歷、添加實(shí)現(xiàn)合并的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
用js實(shí)現(xiàn)的自定義的對(duì)話(huà)框的實(shí)現(xiàn)代碼
javascript alert函數(shù)的替代方案,一個(gè)自定義的對(duì)話(huà)框的方法2010-03-03
Web前端JavaScript中findIndex方法使用示例
這篇文章主要介紹了Web前端JavaScript中findIndex方法使用的相關(guān)資料,findIndex()?返回第一個(gè)符合條件的數(shù)組子項(xiàng)的下標(biāo),找到符合條件的之后就不在繼續(xù)遍歷,需要的朋友可以參考下2025-08-08
javascript中全局對(duì)象的isNaN()方法使用介紹
全局對(duì)象的isNaN()方法通常用于檢測(cè) parseFloat() 和 parseInt() 的結(jié)果,下面為大家介紹下其具體的使用,感興趣的朋友可以參考下2013-12-12
深入淺析JSON.parse()、JSON.stringify()和eval()的作用詳解
這篇文章主要介紹了深入淺析JSON.parse()、JSON.stringify()和eval()的作用詳解的相關(guān)資料,需要的朋友可以參考下2016-04-04
JavaScript直接調(diào)用函數(shù)與call調(diào)用的區(qū)別實(shí)例分析
這篇文章主要介紹了JavaScript直接調(diào)用函數(shù)與call調(diào)用的區(qū)別,結(jié)合額實(shí)例形式分析了JavaScript直接調(diào)用函數(shù)與call調(diào)用的基本用法、區(qū)別及相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-05-05
Javascript實(shí)現(xiàn)快速排序(Quicksort)的算法詳解
排序算法(Sorting algorithm)是計(jì)算機(jī)科學(xué)最古老、最基本的課題之一,要想成為合格的程序員,就必須理解和掌握各種排序算法。2015-09-09

