这是我目前的阵列
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" }}
我正在尝试返回一个新数组,每种货币和每种货币的总价值.
如下所示返回上面数组中每种货币的总金额
0:{currency: "EUR", totalAmount: 3500} 1:{currency: "GBP", totalAmount: 5000} 2:{currency: "USD", totalAmount: 4500} 3:{currency: "INR", totalAmount: 6000}
我的方法最初:
//the current array let theInitialArray = state.vehicle; 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
Ankit Agarwa.. 8
以下是O(n)
获取输出的方法:
首先定义一个空对象tempObj
,该对象将用于根据键将currency
和totalAmount
值存储为对象currency
然后,如果currency
在tempObj
您中定义了此键,则只需添加amount
with totalAmount
用于现有对象.
否则你将创建与一个对象amount
作为totalAmount
,currency
为key
的tempObj
,并currency
作为currency
该项目的forEach
环
最后,您需要这样做,Object.values(tempObj)
以便获取对象值并忽略tempObj
获取所需结果的键.
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);
以下是O(n)
获取输出的方法:
首先定义一个空对象tempObj
,该对象将用于根据键将currency
和totalAmount
值存储为对象currency
然后,如果currency
在tempObj
您中定义了此键,则只需添加amount
with totalAmount
用于现有对象.
否则你将创建与一个对象amount
作为totalAmount
,currency
为key
的tempObj
,并currency
作为currency
该项目的forEach
环
最后,您需要这样做,Object.values(tempObj)
以便获取对象值并忽略tempObj
获取所需结果的键.
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);
您可以Array.reduce()
迭代数据.如果累加器中不存在货币(r
在reduce回调中),则初始化它.将当前金额添加到累加器中的货币金额.使用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);