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ì)象差集的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
javascript FAQ函數(shù)(提問+回復(fù))
javascript FAQ函數(shù),當(dāng)點(diǎn)擊問題時(shí)顯示下面的回復(fù)內(nèi)容。2009-07-07
前端判斷變量不等于undefined不等于null的方法推薦
在前端開發(fā)(JavaScript/TypeScript)中,判斷一個(gè)變量既不等于 undefined 也不等于 null,通常被稱為判斷空值或有效存在,本文給大家推薦了幾種常用的方法,需要的朋友可以參考下2026-02-02
JavaScript制作windows經(jīng)典掃雷小游戲
掃雷是一款相當(dāng)大眾的小游戲,游戲目標(biāo)是在最短的時(shí)間內(nèi)根據(jù)點(diǎn)擊格子出現(xiàn)的數(shù)字找出所有非雷格子,同時(shí)避免踩雷。今天我們來看看如何使用javascript來實(shí)現(xiàn)這款小游戲2015-03-03
前端實(shí)現(xiàn)Word在線預(yù)覽功能詳解
這篇文章主要給大家介紹了關(guān)于前端實(shí)現(xiàn)Word在線預(yù)覽功能的相關(guān)資料,工作中經(jīng)常有時(shí)會(huì)遇到需要給用戶創(chuàng)建word文檔并實(shí)現(xiàn)word文檔在線預(yù)覽的需求,需要的朋友可以參考下2023-09-09
在JavaScript中構(gòu)建ArrayList示例代碼
這篇文章主要介紹了在JavaScript中構(gòu)建ArrayList,很實(shí)用,需要的朋友可以參考下2014-09-09
基于JavaScript實(shí)現(xiàn)簡(jiǎn)單掃雷游戲
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn)簡(jiǎn)單掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
JS之獲取樣式的簡(jiǎn)單實(shí)現(xiàn)方法(推薦)
下面小編就為大家?guī)硪黄狫S之獲取樣式的簡(jiǎn)單實(shí)現(xiàn)方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
理解javascript中try...catch...finally
這篇文章主要幫助大家理解javascript中try...catch...finally,從淺入深,一步步掌握javascript中try...catch...finally的使用方法,感興趣的小伙伴們可以參考一下2015-12-12
js open() 與showModalDialog()方法使用介紹
項(xiàng)目開發(fā)中經(jīng)常要用到j(luò)s open() 與showModalDialog()方法,下面有個(gè)不錯(cuò)的示例,喜歡的朋友可以研究下2013-09-09
TypeScript基礎(chǔ)入門教程之三重斜線指令詳解
這篇文章主要給大家介紹了關(guān)于TypeScript基礎(chǔ)入門教程之三重斜線指令的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10

