Get the total value of the matching array elements

Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
This is my current array
0:modelNumber: "123456789", balance: amount:1000, currency:"EUR"
1:modelNumber: "987654321", balance: amount:2000, currency:"EUR"
2:modelNumber: "322353466", balance: amount:1500, currency:"GBP"
3:modelNumber: "892347522", balance: amount:1000, currency:"USD"
4:modelNumber: "931883113", balance: amount:3000, currency:"INR"
5:modelNumber: "854300564", balance: amount:2500, currency:"GBP"
6:modelNumber: "931883113", balance: amount:3000, currency:"INR"
7:modelNumber: "854300564", balance: amount:3500, currency:"USD"
I'm trying to return a new array, with each currecny and the total value for each currecny.
Like below return the total amount for each currency in the array above
0:currency: "EUR", totalAmount: 3500
1:currency: "GBP", totalAmount: 5000
2:currency: "USD", totalAmount: 4500
3:currency: "INR", totalAmount: 6000
My approach initially
//the current array
let theInitialArray = state.account;
const results = theInitialArray.reduce((accumalator, current) =>
const currency = current.balance;
if (accumalator[currency])
accumalator[currency].push(current);
return accumalator;
accumalator[currency] = [current];
return accumalator;
, );
let frank = Object.keys(results)
let jim = ;
let expectedOutput = theInitialArray.filter((x) =>
for (let i=0; i < frank.length; i++)
if (x.balance.currency === frank[i])
jim.push('currency': frank[i], 'amount': x.balance.amount);
);
console.log('expectedOutput', expectedOutput)
return expectedOutput
javascript arrays
add a comment |Â
up vote
6
down vote
favorite
This is my current array
0:modelNumber: "123456789", balance: amount:1000, currency:"EUR"
1:modelNumber: "987654321", balance: amount:2000, currency:"EUR"
2:modelNumber: "322353466", balance: amount:1500, currency:"GBP"
3:modelNumber: "892347522", balance: amount:1000, currency:"USD"
4:modelNumber: "931883113", balance: amount:3000, currency:"INR"
5:modelNumber: "854300564", balance: amount:2500, currency:"GBP"
6:modelNumber: "931883113", balance: amount:3000, currency:"INR"
7:modelNumber: "854300564", balance: amount:3500, currency:"USD"
I'm trying to return a new array, with each currecny and the total value for each currecny.
Like below return the total amount for each currency in the array above
0:currency: "EUR", totalAmount: 3500
1:currency: "GBP", totalAmount: 5000
2:currency: "USD", totalAmount: 4500
3:currency: "INR", totalAmount: 6000
My approach initially
//the current array
let theInitialArray = state.account;
const results = theInitialArray.reduce((accumalator, current) =>
const currency = current.balance;
if (accumalator[currency])
accumalator[currency].push(current);
return accumalator;
accumalator[currency] = [current];
return accumalator;
, );
let frank = Object.keys(results)
let jim = ;
let expectedOutput = theInitialArray.filter((x) =>
for (let i=0; i < frank.length; i++)
if (x.balance.currency === frank[i])
jim.push('currency': frank[i], 'amount': x.balance.amount);
);
console.log('expectedOutput', expectedOutput)
return expectedOutput
javascript arrays
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
This is my current array
0:modelNumber: "123456789", balance: amount:1000, currency:"EUR"
1:modelNumber: "987654321", balance: amount:2000, currency:"EUR"
2:modelNumber: "322353466", balance: amount:1500, currency:"GBP"
3:modelNumber: "892347522", balance: amount:1000, currency:"USD"
4:modelNumber: "931883113", balance: amount:3000, currency:"INR"
5:modelNumber: "854300564", balance: amount:2500, currency:"GBP"
6:modelNumber: "931883113", balance: amount:3000, currency:"INR"
7:modelNumber: "854300564", balance: amount:3500, currency:"USD"
I'm trying to return a new array, with each currecny and the total value for each currecny.
Like below return the total amount for each currency in the array above
0:currency: "EUR", totalAmount: 3500
1:currency: "GBP", totalAmount: 5000
2:currency: "USD", totalAmount: 4500
3:currency: "INR", totalAmount: 6000
My approach initially
//the current array
let theInitialArray = state.account;
const results = theInitialArray.reduce((accumalator, current) =>
const currency = current.balance;
if (accumalator[currency])
accumalator[currency].push(current);
return accumalator;
accumalator[currency] = [current];
return accumalator;
, );
let frank = Object.keys(results)
let jim = ;
let expectedOutput = theInitialArray.filter((x) =>
for (let i=0; i < frank.length; i++)
if (x.balance.currency === frank[i])
jim.push('currency': frank[i], 'amount': x.balance.amount);
);
console.log('expectedOutput', expectedOutput)
return expectedOutput
javascript arrays
This is my current array
0:modelNumber: "123456789", balance: amount:1000, currency:"EUR"
1:modelNumber: "987654321", balance: amount:2000, currency:"EUR"
2:modelNumber: "322353466", balance: amount:1500, currency:"GBP"
3:modelNumber: "892347522", balance: amount:1000, currency:"USD"
4:modelNumber: "931883113", balance: amount:3000, currency:"INR"
5:modelNumber: "854300564", balance: amount:2500, currency:"GBP"
6:modelNumber: "931883113", balance: amount:3000, currency:"INR"
7:modelNumber: "854300564", balance: amount:3500, currency:"USD"
I'm trying to return a new array, with each currecny and the total value for each currecny.
Like below return the total amount for each currency in the array above
0:currency: "EUR", totalAmount: 3500
1:currency: "GBP", totalAmount: 5000
2:currency: "USD", totalAmount: 4500
3:currency: "INR", totalAmount: 6000
My approach initially
//the current array
let theInitialArray = state.account;
const results = theInitialArray.reduce((accumalator, current) =>
const currency = current.balance;
if (accumalator[currency])
accumalator[currency].push(current);
return accumalator;
accumalator[currency] = [current];
return accumalator;
, );
let frank = Object.keys(results)
let jim = ;
let expectedOutput = theInitialArray.filter((x) =>
for (let i=0; i < frank.length; i++)
if (x.balance.currency === frank[i])
jim.push('currency': frank[i], 'amount': x.balance.amount);
);
console.log('expectedOutput', expectedOutput)
return expectedOutput
javascript arrays
javascript arrays
edited 2 hours ago
Alex Peters
567414
567414
asked 2 hours ago
themanwiththemasterplan
715
715
add a comment |Â
add a comment |Â
6 Answers
6
active
oldest
votes
up vote
3
down vote
Here is a O(n) approach of getting that output:
- You first define a empty object
tempObjwhich will be used to store thecurrencyandtotalAmountvalue as a object based on thecurrencykey - Then, if this
currencykey is defined in thetempObjyou will simply add theamountwith thetotalAmountfor an existing object. - Else you will create a object with the
amountastotalAmount,currencyaskeyoftempObjandcurrencyascurrencyof the item inforEachloop - Finally you will need to do
Object.values(tempObj)so that we get the object values and ignore the keys oftempObjto get the desired result.
var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
var tempObj = ;
arr.forEach((obj)=>
if(tempObj[obj.balance.currency])
tempObj[obj.balance.currency].totalAmount += obj.balance.amount
else
tempObj[obj.balance.currency] =
currency: obj.balance.currency,
totalAmount : obj.balance.amount
);
var resArray = Object.values(tempObj);
console.log(resArray);
2
This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
â malbarmawi
2 hours ago
add a comment |Â
up vote
2
down vote
You can Array.reduce() to iterate the data. If a currency doesn't exist in the accumulator (r in the reduce callback), initialize it. Add the current amount, to the currency amount in the accumulator. Get an array of currencies using Object.values:
const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];
const result = Object.values(data.reduce((r, balance ) =>
const amount, currency = balance;
if(!r[currency]) r[currency] = currency, amount: 0 ;
r[currency].amount += amount;
return r;
, ));
console.log(result);add a comment |Â
up vote
0
down vote
It's possible to reduce the values you have and them map them into the format you require:
const values = [
modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
];
const results = values.reduce((prev, curr) => (
...prev,
[curr.balance.currency]: (prev[curr.balance.currency] ), )
const inCorrectFormat = Object.keys(results).map(key => (
currency: key,
totalAmount: results[key]
))
console.dir(inCorrectFormat)add a comment |Â
up vote
0
down vote
I would first use map to reduce the array elements to just their balance, and then use reduce to make a new object from which I can then map a new array:
let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
let balanceArray = initialArray.map(el => el.balance);
let finalObject = balanceArray.reduce((acc, el) =>
if(acc[el.currency]) acc[el.currency] += el.amount
else acc[el.currency] = el.amount
return acc;
,);
let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));
console.log(finalArray)add a comment |Â
up vote
0
down vote
You could take a Map and render the wanted array of currency/totalAmount pairs as new objects.
var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
result = Array.from(
data.reduce((m, balance: amount, currency ) =>
m.set(currency, (m.get(currency) || 0) + amount), new Map),
([currency, totalAmount]) => ( currency, totalAmount )
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; add a comment |Â
up vote
-1
down vote
My solution with native array methods map, filter and reduce
const res = arr
// map to currency
.map(i => i.balance.currency)
// get unique currency
.filter((v, i, a) => a.indexOf(v) === i)
// map to result
.map((i, a) => (
currency: i,
totalAmount: calcTotalAmount(i)
));
// calculate amount
function calcTotalAmount(currency)
return (
arr
// filter to current curency
.filter(i => i.balance.currency === currency)
// reduce to number
.reduce((accum, i) => accum + i.balance.amount, 0)
);
add a comment |Â
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Here is a O(n) approach of getting that output:
- You first define a empty object
tempObjwhich will be used to store thecurrencyandtotalAmountvalue as a object based on thecurrencykey - Then, if this
currencykey is defined in thetempObjyou will simply add theamountwith thetotalAmountfor an existing object. - Else you will create a object with the
amountastotalAmount,currencyaskeyoftempObjandcurrencyascurrencyof the item inforEachloop - Finally you will need to do
Object.values(tempObj)so that we get the object values and ignore the keys oftempObjto get the desired result.
var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
var tempObj = ;
arr.forEach((obj)=>
if(tempObj[obj.balance.currency])
tempObj[obj.balance.currency].totalAmount += obj.balance.amount
else
tempObj[obj.balance.currency] =
currency: obj.balance.currency,
totalAmount : obj.balance.amount
);
var resArray = Object.values(tempObj);
console.log(resArray);
2
This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
â malbarmawi
2 hours ago
add a comment |Â
up vote
3
down vote
Here is a O(n) approach of getting that output:
- You first define a empty object
tempObjwhich will be used to store thecurrencyandtotalAmountvalue as a object based on thecurrencykey - Then, if this
currencykey is defined in thetempObjyou will simply add theamountwith thetotalAmountfor an existing object. - Else you will create a object with the
amountastotalAmount,currencyaskeyoftempObjandcurrencyascurrencyof the item inforEachloop - Finally you will need to do
Object.values(tempObj)so that we get the object values and ignore the keys oftempObjto get the desired result.
var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
var tempObj = ;
arr.forEach((obj)=>
if(tempObj[obj.balance.currency])
tempObj[obj.balance.currency].totalAmount += obj.balance.amount
else
tempObj[obj.balance.currency] =
currency: obj.balance.currency,
totalAmount : obj.balance.amount
);
var resArray = Object.values(tempObj);
console.log(resArray);
2
This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
â malbarmawi
2 hours ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Here is a O(n) approach of getting that output:
- You first define a empty object
tempObjwhich will be used to store thecurrencyandtotalAmountvalue as a object based on thecurrencykey - Then, if this
currencykey is defined in thetempObjyou will simply add theamountwith thetotalAmountfor an existing object. - Else you will create a object with the
amountastotalAmount,currencyaskeyoftempObjandcurrencyascurrencyof the item inforEachloop - Finally you will need to do
Object.values(tempObj)so that we get the object values and ignore the keys oftempObjto get the desired result.
var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
var tempObj = ;
arr.forEach((obj)=>
if(tempObj[obj.balance.currency])
tempObj[obj.balance.currency].totalAmount += obj.balance.amount
else
tempObj[obj.balance.currency] =
currency: obj.balance.currency,
totalAmount : obj.balance.amount
);
var resArray = Object.values(tempObj);
console.log(resArray);Here is a O(n) approach of getting that output:
- You first define a empty object
tempObjwhich will be used to store thecurrencyandtotalAmountvalue as a object based on thecurrencykey - Then, if this
currencykey is defined in thetempObjyou will simply add theamountwith thetotalAmountfor an existing object. - Else you will create a object with the
amountastotalAmount,currencyaskeyoftempObjandcurrencyascurrencyof the item inforEachloop - Finally you will need to do
Object.values(tempObj)so that we get the object values and ignore the keys oftempObjto get the desired result.
var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
var tempObj = ;
arr.forEach((obj)=>
if(tempObj[obj.balance.currency])
tempObj[obj.balance.currency].totalAmount += obj.balance.amount
else
tempObj[obj.balance.currency] =
currency: obj.balance.currency,
totalAmount : obj.balance.amount
);
var resArray = Object.values(tempObj);
console.log(resArray);var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
var tempObj = ;
arr.forEach((obj)=>
if(tempObj[obj.balance.currency])
tempObj[obj.balance.currency].totalAmount += obj.balance.amount
else
tempObj[obj.balance.currency] =
currency: obj.balance.currency,
totalAmount : obj.balance.amount
);
var resArray = Object.values(tempObj);
console.log(resArray);var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
var tempObj = ;
arr.forEach((obj)=>
if(tempObj[obj.balance.currency])
tempObj[obj.balance.currency].totalAmount += obj.balance.amount
else
tempObj[obj.balance.currency] =
currency: obj.balance.currency,
totalAmount : obj.balance.amount
);
var resArray = Object.values(tempObj);
console.log(resArray);answered 2 hours ago
Ankit Agarwal
20.9k41841
20.9k41841
2
This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
â malbarmawi
2 hours ago
add a comment |Â
2
This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
â malbarmawi
2 hours ago
2
2
This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
â malbarmawi
2 hours ago
This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
â malbarmawi
2 hours ago
add a comment |Â
up vote
2
down vote
You can Array.reduce() to iterate the data. If a currency doesn't exist in the accumulator (r in the reduce callback), initialize it. Add the current amount, to the currency amount in the accumulator. Get an array of currencies using Object.values:
const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];
const result = Object.values(data.reduce((r, balance ) =>
const amount, currency = balance;
if(!r[currency]) r[currency] = currency, amount: 0 ;
r[currency].amount += amount;
return r;
, ));
console.log(result);add a comment |Â
up vote
2
down vote
You can Array.reduce() to iterate the data. If a currency doesn't exist in the accumulator (r in the reduce callback), initialize it. Add the current amount, to the currency amount in the accumulator. Get an array of currencies using Object.values:
const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];
const result = Object.values(data.reduce((r, balance ) =>
const amount, currency = balance;
if(!r[currency]) r[currency] = currency, amount: 0 ;
r[currency].amount += amount;
return r;
, ));
console.log(result);add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can Array.reduce() to iterate the data. If a currency doesn't exist in the accumulator (r in the reduce callback), initialize it. Add the current amount, to the currency amount in the accumulator. Get an array of currencies using Object.values:
const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];
const result = Object.values(data.reduce((r, balance ) =>
const amount, currency = balance;
if(!r[currency]) r[currency] = currency, amount: 0 ;
r[currency].amount += amount;
return r;
, ));
console.log(result);You can Array.reduce() to iterate the data. If a currency doesn't exist in the accumulator (r in the reduce callback), initialize it. Add the current amount, to the currency amount in the accumulator. Get an array of currencies using Object.values:
const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];
const result = Object.values(data.reduce((r, balance ) =>
const amount, currency = balance;
if(!r[currency]) r[currency] = currency, amount: 0 ;
r[currency].amount += amount;
return r;
, ));
console.log(result);const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];
const result = Object.values(data.reduce((r, balance ) =>
const amount, currency = balance;
if(!r[currency]) r[currency] = currency, amount: 0 ;
r[currency].amount += amount;
return r;
, ));
console.log(result);const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];
const result = Object.values(data.reduce((r, balance ) =>
const amount, currency = balance;
if(!r[currency]) r[currency] = currency, amount: 0 ;
r[currency].amount += amount;
return r;
, ));
console.log(result);answered 2 hours ago
Ori Drori
64.1k96682
64.1k96682
add a comment |Â
add a comment |Â
up vote
0
down vote
It's possible to reduce the values you have and them map them into the format you require:
const values = [
modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
];
const results = values.reduce((prev, curr) => (
...prev,
[curr.balance.currency]: (prev[curr.balance.currency] ), )
const inCorrectFormat = Object.keys(results).map(key => (
currency: key,
totalAmount: results[key]
))
console.dir(inCorrectFormat)add a comment |Â
up vote
0
down vote
It's possible to reduce the values you have and them map them into the format you require:
const values = [
modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
];
const results = values.reduce((prev, curr) => (
...prev,
[curr.balance.currency]: (prev[curr.balance.currency] ), )
const inCorrectFormat = Object.keys(results).map(key => (
currency: key,
totalAmount: results[key]
))
console.dir(inCorrectFormat)add a comment |Â
up vote
0
down vote
up vote
0
down vote
It's possible to reduce the values you have and them map them into the format you require:
const values = [
modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
];
const results = values.reduce((prev, curr) => (
...prev,
[curr.balance.currency]: (prev[curr.balance.currency] ), )
const inCorrectFormat = Object.keys(results).map(key => (
currency: key,
totalAmount: results[key]
))
console.dir(inCorrectFormat)It's possible to reduce the values you have and them map them into the format you require:
const values = [
modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
];
const results = values.reduce((prev, curr) => (
...prev,
[curr.balance.currency]: (prev[curr.balance.currency] ), )
const inCorrectFormat = Object.keys(results).map(key => (
currency: key,
totalAmount: results[key]
))
console.dir(inCorrectFormat)const values = [
modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
];
const results = values.reduce((prev, curr) => (
...prev,
[curr.balance.currency]: (prev[curr.balance.currency] ), )
const inCorrectFormat = Object.keys(results).map(key => (
currency: key,
totalAmount: results[key]
))
console.dir(inCorrectFormat)const values = [
modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
];
const results = values.reduce((prev, curr) => (
...prev,
[curr.balance.currency]: (prev[curr.balance.currency] ), )
const inCorrectFormat = Object.keys(results).map(key => (
currency: key,
totalAmount: results[key]
))
console.dir(inCorrectFormat)answered 2 hours ago
OliverRadini
2,013626
2,013626
add a comment |Â
add a comment |Â
up vote
0
down vote
I would first use map to reduce the array elements to just their balance, and then use reduce to make a new object from which I can then map a new array:
let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
let balanceArray = initialArray.map(el => el.balance);
let finalObject = balanceArray.reduce((acc, el) =>
if(acc[el.currency]) acc[el.currency] += el.amount
else acc[el.currency] = el.amount
return acc;
,);
let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));
console.log(finalArray)add a comment |Â
up vote
0
down vote
I would first use map to reduce the array elements to just their balance, and then use reduce to make a new object from which I can then map a new array:
let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
let balanceArray = initialArray.map(el => el.balance);
let finalObject = balanceArray.reduce((acc, el) =>
if(acc[el.currency]) acc[el.currency] += el.amount
else acc[el.currency] = el.amount
return acc;
,);
let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));
console.log(finalArray)add a comment |Â
up vote
0
down vote
up vote
0
down vote
I would first use map to reduce the array elements to just their balance, and then use reduce to make a new object from which I can then map a new array:
let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
let balanceArray = initialArray.map(el => el.balance);
let finalObject = balanceArray.reduce((acc, el) =>
if(acc[el.currency]) acc[el.currency] += el.amount
else acc[el.currency] = el.amount
return acc;
,);
let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));
console.log(finalArray)I would first use map to reduce the array elements to just their balance, and then use reduce to make a new object from which I can then map a new array:
let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
let balanceArray = initialArray.map(el => el.balance);
let finalObject = balanceArray.reduce((acc, el) =>
if(acc[el.currency]) acc[el.currency] += el.amount
else acc[el.currency] = el.amount
return acc;
,);
let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));
console.log(finalArray)let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
let balanceArray = initialArray.map(el => el.balance);
let finalObject = balanceArray.reduce((acc, el) =>
if(acc[el.currency]) acc[el.currency] += el.amount
else acc[el.currency] = el.amount
return acc;
,);
let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));
console.log(finalArray)let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];
let balanceArray = initialArray.map(el => el.balance);
let finalObject = balanceArray.reduce((acc, el) =>
if(acc[el.currency]) acc[el.currency] += el.amount
else acc[el.currency] = el.amount
return acc;
,);
let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));
console.log(finalArray)answered 2 hours ago
Luca Kiebel
6,91831431
6,91831431
add a comment |Â
add a comment |Â
up vote
0
down vote
You could take a Map and render the wanted array of currency/totalAmount pairs as new objects.
var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
result = Array.from(
data.reduce((m, balance: amount, currency ) =>
m.set(currency, (m.get(currency) || 0) + amount), new Map),
([currency, totalAmount]) => ( currency, totalAmount )
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; add a comment |Â
up vote
0
down vote
You could take a Map and render the wanted array of currency/totalAmount pairs as new objects.
var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
result = Array.from(
data.reduce((m, balance: amount, currency ) =>
m.set(currency, (m.get(currency) || 0) + amount), new Map),
([currency, totalAmount]) => ( currency, totalAmount )
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; add a comment |Â
up vote
0
down vote
up vote
0
down vote
You could take a Map and render the wanted array of currency/totalAmount pairs as new objects.
var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
result = Array.from(
data.reduce((m, balance: amount, currency ) =>
m.set(currency, (m.get(currency) || 0) + amount), new Map),
([currency, totalAmount]) => ( currency, totalAmount )
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; You could take a Map and render the wanted array of currency/totalAmount pairs as new objects.
var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
result = Array.from(
data.reduce((m, balance: amount, currency ) =>
m.set(currency, (m.get(currency) || 0) + amount), new Map),
([currency, totalAmount]) => ( currency, totalAmount )
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
result = Array.from(
data.reduce((m, balance: amount, currency ) =>
m.set(currency, (m.get(currency) || 0) + amount), new Map),
([currency, totalAmount]) => ( currency, totalAmount )
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
result = Array.from(
data.reduce((m, balance: amount, currency ) =>
m.set(currency, (m.get(currency) || 0) + amount), new Map),
([currency, totalAmount]) => ( currency, totalAmount )
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; answered 2 hours ago
Nina Scholz
159k1279139
159k1279139
add a comment |Â
add a comment |Â
up vote
-1
down vote
My solution with native array methods map, filter and reduce
const res = arr
// map to currency
.map(i => i.balance.currency)
// get unique currency
.filter((v, i, a) => a.indexOf(v) === i)
// map to result
.map((i, a) => (
currency: i,
totalAmount: calcTotalAmount(i)
));
// calculate amount
function calcTotalAmount(currency)
return (
arr
// filter to current curency
.filter(i => i.balance.currency === currency)
// reduce to number
.reduce((accum, i) => accum + i.balance.amount, 0)
);
add a comment |Â
up vote
-1
down vote
My solution with native array methods map, filter and reduce
const res = arr
// map to currency
.map(i => i.balance.currency)
// get unique currency
.filter((v, i, a) => a.indexOf(v) === i)
// map to result
.map((i, a) => (
currency: i,
totalAmount: calcTotalAmount(i)
));
// calculate amount
function calcTotalAmount(currency)
return (
arr
// filter to current curency
.filter(i => i.balance.currency === currency)
// reduce to number
.reduce((accum, i) => accum + i.balance.amount, 0)
);
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
My solution with native array methods map, filter and reduce
const res = arr
// map to currency
.map(i => i.balance.currency)
// get unique currency
.filter((v, i, a) => a.indexOf(v) === i)
// map to result
.map((i, a) => (
currency: i,
totalAmount: calcTotalAmount(i)
));
// calculate amount
function calcTotalAmount(currency)
return (
arr
// filter to current curency
.filter(i => i.balance.currency === currency)
// reduce to number
.reduce((accum, i) => accum + i.balance.amount, 0)
);
My solution with native array methods map, filter and reduce
const res = arr
// map to currency
.map(i => i.balance.currency)
// get unique currency
.filter((v, i, a) => a.indexOf(v) === i)
// map to result
.map((i, a) => (
currency: i,
totalAmount: calcTotalAmount(i)
));
// calculate amount
function calcTotalAmount(currency)
return (
arr
// filter to current curency
.filter(i => i.balance.currency === currency)
// reduce to number
.reduce((accum, i) => accum + i.balance.amount, 0)
);
answered 2 hours ago
Kliment Ru
3376
3376
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52382319%2fget-the-total-value-of-the-matching-array-elements%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
