简体   繁体   中英

how to update the nested object array in javascript

I would like to know how to update object array in javascript.

how to update original_sample with sample objects sent as argument to the function and perform calculation as shown

here, how to do different calcuation with payfee and without payfee in a single function and update the object

Gotstuck and how to do in javascript.

// update the original_sample with sample2 values
var res = getValue(original_sample, sample2);
// update the original_sample with sample3 values
var res = getValue(original_sample, sample3);
function getValue(original, sample){
return original.map(({ fee, rate, ...others }) => ({
    id: 'trans'
    amount: amount,
    fee: +fee,
    rate: +rate,
    targetamount: (amount-fee)*rate
  }))
}
//inputs
var original_sample= [{
     id: "trans",
     fee: 1,
     rate: 2.2,
     amount:100,
  }]
//passed as input to update original_sample
var sample2 = [{
   id: "trans",
   fee: 2,
   rate: 4.0,
   payfee: 1%,
   amount: 100,
  }]
//passed as input to update original_sample
var sample3 =[{
 id: "trans",
 fee: 1,
 rate: 1.0,
 amount: 100
   }]
Expected Result:
//getValue(original_sample, sample2);
res= [{
   id: "trans"
   fee: 2,
   rate: 4.0,
   payfee: 1,
   amount: 100,
targetamount: 388.08
    }]

//getValue(original_sample, sample3);
res=[{
 id: "trans",
 fee: 1,
 rate: 1.0,
 amount: 100,
targetamount: 99
   }]

As you only have one object in each array, I would either abandon the use of arrays completely, or extract the object every time you have to do something. For instance like this:

 function getValue(orig, update) { var obj = orig[0] = Object.assign({}, update[0]); if ("payfee" in obj) obj.payfee = parseInt(obj.payfee); obj.targetamount = (obj.amount-obj.fee) * (1 - (obj.payfee||0)/100) * obj.rate; } // Demo: var original_sample = [{id: "trans", fee: 1, rate: 2.2, amount:100}]; var sample2 = [{id: "trans", fee: 2, rate: 4.0, payfee: "1%", amount: 100}]; var sample3 = [{id: "trans", fee: 1, rate: 1.0, amount: 100}]; getValue(original_sample, sample2); console.log(original_sample); getValue(original_sample, sample3); console.log(original_sample); 

Use Array.concat() to combine the arrays, and then reduce the combined array to an object, by always taking the last object with the same id and calculating the targetAmount . Convert back to an array with Object.values() .

 const getValue = (original, sample) => Object.values( original.concat(sample) .reduce((r, o) => ({ ...r, [o.id]: { ...o, targetamount: (o.amount - o.fee) * o.rate } }) , {}) ) const original_sample = [{"id":"trans","fee":1,"rate":2.2,"amount":100}] const sample2 = [{"id":"trans","fee":2,"rate":4,"payfee":0.01,"amount":100}] const sample3 = [{"id":"trans","fee":1,"rate":1,"amount":100}] const originalAnd2 = getValue(original_sample, sample2) const original2And3 = getValue(originalAnd2, sample3) console.log(originalAnd2); console.log(original2And3); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM