当前位置:  开发笔记 > 编程语言 > 正文

获取匹配数组元素的总值

如何解决《获取匹配数组元素的总值》经验,为你挑选了2个好方法。

这是我目前的阵列

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,该对象将用于根据键将currencytotalAmount值存储为对象currency

    然后,如果currencytempObj您中定义了此键,则只需添加amountwith totalAmount用于现有对象.

    否则你将创建与一个对象amount作为totalAmount,currencykeytempObj,并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);



1> Ankit Agarwa..:

以下是O(n)获取输出的方法:

    首先定义一个空对象tempObj,该对象将用于根据键将currencytotalAmount值存储为对象currency

    然后,如果currencytempObj您中定义了此键,则只需添加amountwith totalAmount用于现有对象.

    否则你将创建与一个对象amount作为totalAmount,currencykeytempObj,并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);


2> Ori Drori..:

您可以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);
推荐阅读
小白也坚强_177
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有