如何比较两个数组以及删除、更新数组中的元素



我有两个对象数组,如

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。现在通过比较两个数组如果数组b中缺少任何收费那么在数组a中添加一个布尔属性就像在数组a中银行收费记录在那里,但它在数组b中没有所以我必须为数组a中的银行收费对象添加一个属性

  1. 在数组B中,如果任何记录被更新,这些值应该被复制到数组a中,就像在数组B中处理收费记录ct,st及其值被更新一样,这些值应该被更新到数组a中。

有谁能帮我一下吗

根据要求

1。现在通过比较两个数组,如果数组b中缺少任何费用,那么在数组a中添加一个布尔属性,就像数组a中的银行费用一样record在这里,但它在数组b中不见了,所以我必须添加a属性赋给数组A中的银行收费对象。在数组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不变并创建一个新对象:

c = Object.assign({},a,b);

最新更新