计算来自对象数组的特定数据

时间:2018-04-24 02:57:00

标签: javascript arrays dictionary

我想根据用户输入的颜色计算条目数,就像用户输入红色一样,程序应该返回11.这就是我试过的。循环后没有定义变量cnt。



let description=[
                {color:'red',qty:6,remarks:'asdf'},
                {color:'green',qty:5,remarks:'asdf'},
                {color:'red',qty:5,remarks:'asdf'},                             
                {color:'yellow',qty:5,remarks:'asdf'},                         
                {color:'blue',qty:5,remarks:'asdf'},                           
                {color:'white',qty:5,remarks:'asdf'}
                ];
{description.map((t,index) =>{
         let cnt=0;
         if(t.color=='red'){
            cnt=cnt+parseInt(t.qty);    
         }
         console.log(cnt);
      }   
      )}
 console.log(cnt);     




6 个答案:

答案 0 :(得分:3)

您的代码中的问题是您在函数内部和每次重复时初始化let cnt=0;,这意味着您实际上并未添加值,因为您在每个循环中将cnt重置为0。 / p>

您可以使用reduce并测试颜色是否为colorToSearch,如果相同则添加累加器和当前数量值。



let description=[
     {color:'red',qty:6,remarks:'asdf'},
     {color:'green',qty:5,remarks:'asdf'},
     {color:'red',qty:5,remarks:'asdf'},                             
     {color:'yellow',qty:5,remarks:'asdf'},                         
     {color:'blue',qty:5,remarks:'asdf'},                           
     {color:'white',qty:5,remarks:'asdf'}
];

let colorToSearch = 'red';

let totalQty = description.reduce((c, v) => v.color === colorToSearch ? c + v.qty : c, 0)

console.log(totalQty);




答案 1 :(得分:0)

您可以先过滤数组,然后像下面那样应用reduce



let description=[
                {color:'red',qty:6,remarks:'asdf'},
                {color:'green',qty:5,remarks:'asdf'},
                {color:'red',qty:5,remarks:'asdf'},                             
                {color:'yellow',qty:5,remarks:'asdf'},                         
                {color:'blue',qty:5,remarks:'asdf'},                           
                {color:'white',qty:5,remarks:'asdf'}
                ];
                
 let s=description.filter(description => description.color === 'red').reduce(function(sum, currentValue) {
                 return sum + (parseInt(currentValue.qty) || 0);},0)
 console.log(s);               




答案 2 :(得分:0)

您可以按如下方式使用Array.prototype.reduce()



const description=[
  {color:'red',qty:6,remarks:'asdf'},
  {color:'green',qty:5,remarks:'asdf'},
  {color:'red',qty:5,remarks:'asdf'},                             
  {color:'yellow',qty:5,remarks:'asdf'},                         
  {color:'blue',qty:5,remarks:'asdf'},                           
  {color:'white',qty:5,remarks:'asdf'}
];
const color = 'red';
const qty = description.reduce((a, v) => a + (v.color == color ? v.qty : 0), 0);

console.log(qty);




答案 3 :(得分:0)

您可以使用数组过滤器或数组减少方法。下面是数组reduce的示例。

在你的程序中,let cnt=0;位于map函数内。因此,cnt语句

中将无法使用console.log的值



let description = [{
    color: 'red',
    qty: 6,
    remarks: 'asdf'
  },
  {
    color: 'green',
    qty: 5,
    remarks: 'asdf'
  },
  {
    color: 'red',
    qty: 5,
    remarks: 'asdf'
  },
  {
    color: 'yellow',
    qty: 5,
    remarks: 'asdf'
  },
  {
    color: 'blue',
    qty: 5,
    remarks: 'asdf'
  },
  {
    color: 'white',
    qty: 5,
    remarks: 'asdf'
  }
];



function getCount(matchedColor) {
  return description.reduce(function(acc, curr) {
    //Now checking if the color is matching with supplied paramenter
    if (curr.color === matchedColor) {
      acc += curr.qty // add with previous value
    };
    return acc
  }, 0) // zero is initial value, that is initially the sum is zero
}
console.log(getCount('red'));




答案 4 :(得分:0)

您的变量ctn在每次迭代时都会重新定义,并且只存在于.map函数中。这就是您收到错误的原因"循环结束后,变量cnt未定义"

您需要做的就是将ctn移到.map之外,它会起作用:



let description=[
                {color:'red',qty:6,remarks:'asdf'},
                {color:'green',qty:5,remarks:'asdf'},
                {color:'red',qty:5,remarks:'asdf'},                             
                {color:'yellow',qty:5,remarks:'asdf'},                         
                {color:'blue',qty:5,remarks:'asdf'},                           
                {color:'white',qty:5,remarks:'asdf'}
                ];
                
let cnt=0;    

description.map((t,index) => {
         if(t.color=='red'){
            cnt=cnt+parseInt(t.qty); //Note: parseInt() isn't needed if qty is always a number
         }
      });
      
 console.log(cnt);




答案 5 :(得分:0)

我个人认为使用filter然后使用reduce来获取总和是一种更具可读性的解决方案。

let description = [
  {color:'red',qty:6,remarks:'asdf'},
  {color:'green',qty:5,remarks:'asdf'},
  {color:'red',qty:5,remarks:'asdf'},                             
  {color:'yellow',qty:5,remarks:'asdf'},                         
  {color:'blue',qty:5,remarks:'asdf'},                           
  {color:'white',qty:5,remarks:'asdf'}
];
let result = description.filter(item => item.color === 'red').reduce((prev, {qty}) => prev + qty, 0);
console.log(result);