Kenzo
Kenzo

Reputation: 1837

How to efficiently evaluate element in javascript array by using reduce function

I have an array called people which is an array of objects(person's name and his/her city's name), and i want to make a function that calculate the total number of distinct cities in that array. I used a function that use for loop but it seems to be a better way by using reduce functions in javascript. Here is the snippet

const people = [
  { name: "Jessica", city: "New York"},
  { name: "Steve",   city: "Los Angels"},
  { name: "Peter",   city: "Boston"},
  { name: "Elaine",  city: "Montreal"},
  { name: "Chris",  city: "Montreal"},
  { name: "Mike",  city: "Boston"},
  { name: "George",  city: "Vancouver"},
];
let nbre_distinct_cities = 0;

countDistinctCity(people);
console.log('Total number of distinct cities: ',nbre_distinct_cities); 

function countDistinctCity(people)
{
  for(let i = 0; i < people.length; i++)
  {
      if(i === people.length - 1)
      {
          break;
      }
      else if(people[i].city !== people[i + 1].city)
      {
        nbre_distinct_cities++
      }
  }

}

I would appreciate if someone suggest an efficient function using reduce() function

Upvotes: 1

Views: 210

Answers (4)

Karim
Karim

Reputation: 8632

An alternative solution that removes the duplicates with Array.indexOf

const people = [
  { name: "Jessica", city: "New York"},
  { name: "Steve",   city: "Los Angels"},
  { name: "Peter",   city: "Boston"},
  { name: "Elaine",  city: "Montreal"},
  { name: "Chris",  city: "Montreal"},
  { name: "Mike",  city: "Boston"},
  { name: "George",  city: "Vancouver"},
];

let nbre_distinct_cities = people.map(el => el.city)
 .filter((city, idx, arr) => arr.indexOf(city) === idx).length;

console.log('Total number of distinct cities: ', nbre_distinct_cities); 

Upvotes: 0

jjbskir
jjbskir

Reputation: 10647

Using reduce

Object.keys(people.reduce((acc, ppl) => (acc[ppl.city] = ppl.city, acc), {})).length

Upvotes: 1

Max
Max

Reputation: 799

you can solve the problem via reduce method

    const cities = people.reduce((accumulator, current) => {
       const isItNotExistInAccumulator
          = accumulator.every(city => city !== current.city);
       if (isItNotExistInAccumulator) return [current.city, ...accumulator];
       return accumulator;
    }, []);

    console.log(cities.length);

Upvotes: 0

slider
slider

Reputation: 12990

You can use a Set to store all the cities from the array and, since a set only has unique entries, the final size of the set will give you the number of distinct cities:

const people = [
  { name: "Jessica", city: "New York"},
  { name: "Steve",   city: "Los Angels"},
  { name: "Peter",   city: "Boston"},
  { name: "Elaine",  city: "Montreal"},
  { name: "Chris",  city: "Montreal"},
  { name: "Mike",  city: "Boston"},
  { name: "George",  city: "Vancouver"},
];

let nbre_distinct_cities = new Set(people.map(({city}) => city)).size;
console.log('Total number of distinct cities: ', nbre_distinct_cities); 

Upvotes: 6

Related Questions