George Huang
George Huang

Reputation: 27

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:

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?

Upvotes: 1

Views: 74

Answers (3)

LearningEveryday
LearningEveryday

Reputation: 584

You can use the existing object as well if you want and use the same for the graph and any other place if needed.

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
  }
]

Upvotes: 1

Lu&#237;s Ramalho
Lu&#237;s Ramalho

Reputation: 10208

You could use map() and do something like:

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:

  1. currentValue - The current element being processed in the array;
  2. index - The index of the current element being processed in the array:
  3. array - The array map was called upon.

So you can use those parameters to achieve the same thing you've done with your for loop.

The reduce() is not really what you're looking for in this case because you want more than a single output value.

Upvotes: 2

Abolfazl
Abolfazl

Reputation: 1700

You can use inline If Statement as follows:

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
    });
}

Upvotes: 0

Related Questions