我正在嘗試根據兩個物件陣列的比較結果來獲取具有不同專案的單個物件。id
情況如下:
第一個陣列
[
{
"id": "792657571767910421",
"type": 0,
"allow": "0",
"deny": "0"
},
{
"id": "1020443938668171294",
"type": 0,
"allow": "0",
"deny": "377959221312"
},
{
"id": "791708642813411358",
"type": 0,
"allow": "0",
"deny": "0"
}
]
第二個陣列
[
{
"id": "792657571767910421",
"type": 0,
"allow": "1024",
"deny": "0"
},
{
"id": "1020443938668171294",
"type": 0,
"allow": "0",
"deny": "377959221312"
},
{
"id": "791708642813411358",
"type": 0,
"allow": "0",
"deny": "0"
},
]
預期產出
[
{
"id": "792657571767910421",
"type": 0,
"allow": "1024",
"deny": "0"
},
]
不同的價值
// First array
{
"allow": "0"
}
// Second array
{
"allow": "1024"
}
誰能幫我?謝謝你。
uj5u.com熱心網友回復:
您將不得不使用自定義過濾器邏輯:
邏輯:
- 回圈
arr2或您需要輸出的陣列。 - 帶有 的測驗專案
param2。比賽中:- 回圈
arr1或其他陣列。 - 查找具有相同 ID 的物件。
- 發現時,
- 帶有 的測驗專案
param1。回傳測驗值。
- 帶有 的測驗專案
- 否則回傳假
- 回圈
function filter(arr1, arr2, param1, param2) {
const test = (param, item) =>
Object
.entries(param)
.every(([k, v]) => item[k] === v)
return arr2.filter((itemB) => {
if (test(param2, itemB)) {
const itemA = arr1.find(({id}) => id === itemB.id)
return !!itemA ? test(param1, itemA) : false
}
return false
})
}
const arr1 = [{"id": "792657571767910421","type": 0,"allow": "0","deny": "0"},{"id": "1020443938668171294","type": 0,"allow": "0","deny": "377959221312"},{"id": "791708642813411358","type": 0,"allow": "0","deny": "0"}]
const arr2 = [{"id": "792657571767910421","type": 0,"allow": "1024","deny": "0"},{"id": "1020443938668171294","type": 0,"allow": "0","deny": "377959221312"},{"id": "791708642813411358","type": 0,"allow": "0","deny": "0"},]
const param1 = {"allow": "0"}
const param2 = {"allow": "1024"}
console.log(filter(arr1, arr2, param1, param2))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/513978.html
上一篇:為打字稿中的物件制作物件
