![](/img/trans.png)
[英]How to compare two arrays and add/remove elements depending on their difference?
[英]how to compare two arrays and remove, update elements of array
我有兩個 arrays 之類的對象
a = [
{chargeType: "Accounting Charges", ct: 6, st: 6, it: 12},
{chargeType: "Commission", ct: 6, st: 6, it: 12},
{chargeType: "Processing Charges", ct: 6, st: 6, it: 12},
{chargeType: "Verification Charges", ct: 6, st: 6, it: 12},
{chargeType: "Application Fees", ct: 6, st: 6, it: 12},
{chargeType: "Legal Charges", ct: 6, st: 6, it: 12},
{chargeType: "Bank Charges", ct: 6, st: 6, it: 12},
]
和
b = [
{chargeType: "Accounting Charges", ct: 6, st: 6, it: 12},
{chargeType: "Commission", ct: 6, st: 6, it: 12},
{chargeType: "Processing Charges", ct: 7, st: 7, it: 14},
{chargeType: "Verification Charges", ct: 6, st: 6, it: 12},
{chargeType: "Application Fees", ct: 6, st: 6, it: 12},
{chargeType: "Legal Charges", ct: 6, st: 6, it: 12},
]
1.現在通過比較 arrays 如果數組 b 中缺少任何費用,則在數組 a 中添加 boolean 屬性。 就像數組 A 中的銀行費用記錄在那里一樣,但數組 B 中缺少它。所以,我必須在數組 A 中向該銀行費用 object 添加一個屬性。
任何人都可以幫助如何實現這一目標
根據所問
1.現在通過比較 arrays 如果數組 b 中缺少任何費用,則在數組 a 中添加 boolean 屬性。 就像在數組 A 銀行費用記錄中一樣,但它在數組 B 中丟失。所以,我必須在數組 A 中向該銀行費用 object 添加一個屬性。在數組 B 中,如果更新了任何記錄,這些值應復制到數組 A 中. 就像在數組 B 中處理費用記錄 ct,st 並且它的值被更新並且這些值應該在數組 A 中更新。
這可以通過一行來完成:
Object.assign(x,y) // x <-- what was in x, updated with what was in y
我正在更改a
中的一個值,以表明它已使用來自b
的內容進行了更新
a = [ {chargeType: "Accounting Charges", ct: 111111111111, st: 6, it: 12}, {chargeType: "Commission", ct: 6, st: 6, it: 12}, {chargeType: "Processing Charges", ct: 6, st: 6, it: 12}, {chargeType: "Verification Charges", ct: 6, st: 6, it: 12}, {chargeType: "Application Fees", ct: 6, st: 6, it: 12}, {chargeType: "Legal Charges", ct: 6, st: 6, it: 12}, {chargeType: "Bank Charges", ct: 6, st: 6, it: 12}, ] b = [ {chargeType: "Accounting Charges", ct: 6, st: 6, it: 12}, {chargeType: "Commission", ct: 6, st: 6, it: 12}, {chargeType: "Processing Charges", ct: 7, st: 7, it: 14}, {chargeType: "Verification Charges", ct: 6, st: 6, it: 12}, {chargeType: "Application Fees", ct: 6, st: 6, it: 12}, {chargeType: "Legal Charges", ct: 6, st: 6, it: 12}, ] Object.assign(a,b) console.log(a)
如果您想a
原樣並創建一個新的 object:
c = Object.assign({},a,b);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.