简体   繁体   English

如何在 JavaScript 中计算没有 for 循环的对象数组中的值差异?

[英]How to calculate values differences in an array of objects without a for loop, in JavaScript?

Say I have an array of objects:假设我有一个对象数组:

const peopleArray = [
{
 'date': '3/1',
 'people': 0
},
{
 'date: '3/2',
 'people': 5
},
{
 'date: '3/3',
 'people': 8
},
...
];

I'm trying to draw a chart showing daily difference between the number of people, so I need the following array of objects:我正在尝试绘制一个显示每日人数差异的图表,因此我需要以下对象数组:

[{ 
 'date': '3/1,
 'diff': 0 // Shows 0 the first day.
},
{
 'date': '3/2',
 'diff': 5 // 5 people joined.
},
{
 'date': '3/3',
 'diff': 3 // 3 more people joined.
},
...
];

I know I can achieve this with Array.push() and a for loop:我知道我可以使用Array.push()和 for 循环来实现这一点:

 const peopleArray = [{ date: '3/1', people: 0 }, { date: '3/2', people: 5 }, { date: '3/3', people: 8 } ]; let diffArray = []; for (i = 0; i < peopleArray.length; i++) { if (i === 0) { // Shows 0 the first day diffArray.push({ date: peopleArray[i].date, diff: 0 }); } else { diffArray.push({ date: peopleArray[i].date, diff: peopleArray[i].people - peopleArray[i - 1].people }); } } console.log(diffArray);

But is there a cleaner way to do this with map() , reduce() or any of those array functions?但是有没有更简洁的方法来使用map()reduce()或任何这些数组函数?

You could use map() and do something like:您可以使用map()并执行以下操作:

 const peopleArray = [ { date: "3/1", people: 0, }, { date: "3/2", people: 5, }, { date: "3/3", people: 8, }, ]; let diffArray = peopleArray.map((o, i, a) => ({ date: o.date, diff: i > 0? o.people - a[i - 1].people: 0, })); console.log(diffArray);

The map() takes a callback function that accepts three arguments: map()接受一个回调 function,它接受三个 arguments:

  1. currentValue - The current element being processed in the array; currentValue - 数组中正在处理的当前元素;
  2. index - The index of the current element being processed in the array: index - 数组中正在处理的当前元素的索引:
  3. array - The array map was called upon. array - 阵列 map 被调用。

So you can use those parameters to achieve the same thing you've done with your for loop.因此,您可以使用这些参数来实现您对 for 循环所做的相同事情。

The reduce() is not really what you're looking for in this case because you want more than a single output value.在这种情况下, reduce()并不是您真正需要的,因为您需要的不仅仅是一个 output 值。

You can use the existing object as well if you want and use the same for the graph and any other place if needed.如果需要,您也可以使用现有的 object,并将其用于图形和任何其他地方(如果需要)。

 const peopleArray = [{ date: '3/1', people: 0 }, { date: '3/2', people: 5 }, { date: '3/3', people: 8 } ]; var peopleArrayLength = peopleArray.length; if(peopleArrayLength > 0){ var peopleObject = peopleArray[0]; peopleObject.diff = 0; peopleArray[0] = peopleObject; for (i = 1; i < peopleArrayLength; i++) { peopleObject = peopleArray[i]; peopleObject.diff = peopleArray[i].people - peopleArray[i-1].people; peopleArray[i] = peopleObject; } } console.log(peopleArray);

Outputs:输出:

[
  {
    "date": "3/1",
    "people": 0,
    "diff": 0
  },
  {
    "date": "3/2",
    "people": 5,
    "diff": 5
  },
  {
    "date": "3/3",
    "people": 8,
    "diff": 3
  }
]

You can use inline If Statement as follows:您可以使用内联 If 语句,如下所示:

for (i = 0; i < peopleArray.length; i++) {
    var df=i!=0?(peopleArray[i].people - peopleArray[i - 1].people):0;
    diffArray.push({
      date: peopleArray[i].date,
      diff: df
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM