简体   繁体   中英

Need help performing (multiplication) operations on object arrays using Javascript functions

I need to know how to divide values from a particular column from two different object arrays. Price, Cash, cid are the arguments we provide while calling the function cash_register(). cid is the total amount of currency notes each that exist in the register currently. price is the amount to be paid and cash is the cash given by the customer. First array consists of the total amount of currency units i have and the second array contains the actual value of each currency unit. In my denom function I'm trying to get an array which will show how many of each currency units I own. Currency units here stand for Penny, Nickel, Dime, Quarter etc. in my cid variable.

I'm unsure if I've used the map function incorrectly or if i should try a different method or should I just convert my object arrays instead of just retrieving the values.

 //INPUT - 2 D array of cash with the total amount left var cid = [ ["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100] ]; function cash_register(cid, price, cash) { //curr_units defines the amount of each currency unit var curr_units = { "PENNY": 0.01, "NICKEL": 0.05, "DIME": 0.1, "QUARTER": 0.25, "ONE": 1, "FIVE": 5, "TEN": 10, "TWENTY": 20, "ONE HUNDRED": 100 }; var change = cash - price; // only values of cid var new_cid = []; for (var i = 0; i < cid.length; i++) { new_cid.push(cid[i][1]); } // can't figure this part out. Need to divide cid values by curency unit values var getDenom = function(new_cid, curr_units) { return Object.values(curr_units).map((x) => (new_cid[x] / x)); } } 

The output I need is an object array with key value pairs of currency unit and the no of denominations of each.

denom={
  "PENNY": 101,
  "NICKEL": 41,
  "DIME": 31,
  "QUARTER": 17,
  "ONE": 90,
  "FIVE": 11,
  "TEN": 2,
  "TWENTY": 3,
  "ONE HUNDRED": 1};

I think you are trying to do this:

Use Object.keys() instead of Object.values()

// Fill the array like this [key:value]
for (var i = 0; i < cid.length; i++)
{
  new_cid[cid[i][0]] = cid[i][1];
}

// Map on the keys and not the values
var getDenom = function(new_cid, curr_units)
{
  return Object.keys(curr_units).map(x => new_cid[x] / curr_units[x]);
}

Edit : Another solution with Array.forEach , simpler than using for :

No need of new_cid

var result = [];
cid.forEach(item => {
  result[item[0]] = item[1] / curr_units[item[0]];
});

console.log(result);

You can do this using one simple loop.

 var cid = [ ["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100] ]; var curr_units = { "PENNY": 0.01, "NICKEL": 0.05, "DIME": 0.1, "QUARTER": 0.25, "ONE": 1, "FIVE": 5, "TEN": 10, "TWENTY": 20, "ONE HUNDRED": 100 }; function calculate(cid, units) { var total = {}; cid.forEach(val => { total[val[0]] = Math.round(val[1] / units[val[0]]); }); return total; } 

Yields:

{
  "PENNY": 101,
  "NICKEL": 41,
  "DIME": 31,
  "QUARTER": 17,
  "ONE": 90,
  "FIVE": 11,
  "TEN": 2,
  "TWENTY": 3,
  "ONE HUNDRED": 1
}

With an array of objects with a key/value pair you can address it simply. Here I present a slightly verbose example so you can tell what is happening.

 //INPUT - 2 D array of cash with the total amount left var cid = [ ["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100] ]; var getDenom = function(ncid, units) { let ret = Object.keys(units).map(function(key, index) { // console.log("key:",key);// the name like PENNY // console.log("ukey:",units[key]); // the value like 0.01 // console.log("nkey:",ncid[index]); // the value like `1.01 let p = { k: key, val: Math.round(ncid[index] / units[key]) }; // console.log(p); return p; }); return ret; }; function cash(cid, price, change) { //curr_units defines the amount of each currency unit var curr_units = { "PENNY": 0.01, "NICKEL": 0.05, "DIME": 0.1, "QUARTER": 0.25, "ONE": 1, "FIVE": 5, "TEN": 10, "TWENTY": 20, "ONE HUNDRED": 100 }; // why redefine change and what is cash? commented out as it is unknown what the intent is // var change = cash - price; // only values of cid var new_cid = []; for (var i = 0; i < cid.length; i++) { new_cid.push(cid[i][1]); } let den = getDenom(new_cid, curr_units); //console.log("den:",den); let robj = {}; den.forEach(function(element) { robj[element.k] = element.val }); // console.log("den:", den); // same as "ret" above //console.log('robj:', robj); return robj; } let newObj = cash(cid, 0, 0); console.log("newObj:", newObj); 

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