js獲取兩個(gè)數(shù)組對(duì)象的差集實(shí)現(xiàn)方法
獲取兩個(gè)數(shù)組對(duì)象中都沒不包含的元素

獲得兩個(gè)數(shù)組對(duì)象的差集,就是獲取兩個(gè)數(shù)組對(duì)象中都沒不包含的元素:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>獲得兩個(gè)數(shù)組對(duì)象的差集</title>
<!--引入 element-ui 的樣式,-->
<link rel="stylesheet" rel="external nofollow" >
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
<!-- 引入element 的組件庫-->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
#app {
margin: 50px;
}
.item {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div id="app">
<div class="item">
原數(shù)組:
<div v-for="(item, index) in selectedText" :key="index">{{item}}</div>
</div>
<div class="item">
和原數(shù)組對(duì)比的數(shù)組:
<div v-for="(item, index) in brightTextArr" :key="index">{{item}}</div>
</div>
<div class="item">
差集:
<div v-for="(item, index) in differenceSet" :key="index">{{item}}</div>
</div>
</div>
<script>
new Vue({
el: '#app',
data() {
return {
// 原數(shù)組
selectedText: [
{
"parentId": "1665538708874002433",
"childId": [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7"
]
},
{
"parentId": "1665538708861419522",
"childId": [
"0",
"1",
"2",
"3",
"4",
"5"
]
}
],
// 要對(duì)比的數(shù)組
brightTextArr: [
{
"parentId": "1665538708861419522",
"childId": [
"3",
"4",
"5",
'9'
]
},
{
"parentId": "1665538708869808130",
"childId": [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7"
]
}
],
// 差集
differenceSet: []
}
},
created() {
this.subtractionFun(this.selectedText, this.brightTextArr)
},
methods: {
subtractionFun(selectedText, brightTextArr) {
this.differenceSet = JSON.parse(JSON.stringify(brightTextArr));
// 轉(zhuǎn)換成對(duì)象,parentId為屬性名,childId為屬性值,
// 格式為 { 1665538708861419522: ['3', '4', '5', '9'] }
const brightTextMap = this.differenceSet.reduce((pre, cur) => {
return {
...pre,
[cur.parentId]: cur.childId,
};
}, {});
selectedText.forEach((item, index) => {
const childId = item.childId || [];
const parentChildId = brightTextMap[item.parentId] || [];
// 求兩個(gè)數(shù)組的差集
const subtraction = parentChildId.filter((v) => !childId.includes(v));
const parentChildIdIndex = this.differenceSet.findIndex(
(v) => v.parentId === item.parentId
);
if (parentChildIdIndex >= 0) {
// 找到和原數(shù)組有相同有parentId并且有差集的那一項(xiàng),替換成差集
this.differenceSet[parentChildIdIndex].childId = subtraction;
}
});
console.log(this.differenceSet, '差集')
},
}
})
</script>
</body>
</html>遇到的問題
1.將要對(duì)比的數(shù)組對(duì)象轉(zhuǎn)換成以parentId為屬性名,childId為屬性值格式的對(duì)象,這樣好對(duì)比。
格式為 { 1665538708861419522: ['3', '4', '5', '9'] }
2.需要找到和原數(shù)組有相同有parentId并且有差集的那一項(xiàng),替換成差集
以上就是js獲取兩個(gè)數(shù)組對(duì)象的差集實(shí)現(xiàn)方法的詳細(xì)內(nèi)容,更多關(guān)于js獲取兩個(gè)數(shù)組對(duì)象差集的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
javascript獲取wx.config內(nèi)部字段解決微信分享
這篇文章主要介紹了javascript獲取wx.config內(nèi)部字段解決微信分享,需要的朋友可以參考下2016-03-03
JS正則表達(dá)式判斷有效數(shù)實(shí)例代碼
這篇文章主要介紹了JS正則表達(dá)式判斷有效數(shù)實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
javascript offsetX與layerX區(qū)別
FF沒有offsetX屬性,有個(gè)layerX屬性,只要將事件源的位置設(shè)置成相對(duì)定位(position:relative)或絕對(duì)定位(position:absolute),兩者結(jié)果就相等,表示事件源相對(duì)于父元素的X坐標(biāo)。2010-03-03
用原生JS對(duì)AJAX做簡單封裝的實(shí)例代碼
下面小編就為大家?guī)硪黄迷鶭S對(duì)AJAX做簡單封裝的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
如何制作浮動(dòng)廣告 JavaScript制作浮動(dòng)廣告代碼
如果有一定的JavaScript基礎(chǔ),制作浮動(dòng)廣告還是比較容易的,利用閑暇時(shí)間簡單制作了一個(gè),感興趣的朋友可以參考下哦2012-12-12

