-2

I have two arrays of objects like

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},
]

and

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.now by comparing both arrays If any charges missing in array b then add a boolean property in array a. like in array A bank charges record is there, but it's missing in array B. so, I have to add a property to that bank charges object in array A.

  1. in Array B if any record is updated those values should be copied in array A. like in array B proceesing charges record ct,st and it values are updated and those values should be updated in array A.

can anyone help how to achieve this

sun
  • 1,832
  • 4
  • 19
  • 31

1 Answers1

0

According to what's asked

1.now by comparing both arrays If any charges missing in array b then add a boolean property in array a. like in array A bank charges record is there, but it's missing in array B. so, I have to add a property to that bank charges object in array A. in Array B if any record is updated those values should be copied in array A. like in array B proceesing charges record ct,st and it values are updated and those values should be updated in array A.

This can be done with one line:

Object.assign(x,y) // x <-- what was in x, updated with what was in y

I'm changing one value in a for showing that it is updated with what comes from 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)

If you want to keep a as it is and create a new object:

c = Object.assign({},a,b);
malarres
  • 2,941
  • 1
  • 21
  • 35