Home > Back-end >  Using reduce method in an array JavaScript
Using reduce method in an array JavaScript

Time:01-05

import { data } from '../data/data';

export function getGreatestDiscoveryYear(data) {
  const getGDY = data.asteroids;
  const results = getGDY.filter(function (getAsteroids) {
    return getAsteroids.discoveryYear;
  }).map(function (getAsteroidYear) {
    return getAsteroidYear.discoveryYear;
  }).reduce(function (getYear) {
    return getYear
  })
  return results;
}

export const data = {
   asteroids: [
    { name: '1 Ceres', discoveryYear: 1801, orbitalPeriod: 1681.63 },
    { name: '6 Hebe', discoveryYear: 1847, orbitalPeriod: 1380.373 },
    { name: '47171 Lempo', discoveryYear: 1999, orbitalPeriod: 91678 },
    { name: '762 Pulcova', discoveryYear: 1913, orbitalPeriod: 2049.475 },
    { name: '4179 Toutatis', discoveryYear: 1989, orbitalPeriod: 1463.14 },
    { name: '50000 Quaoar', discoveryYear: 2002, orbitalPeriod: 104347.575 },
    { name: '2867 Šteins', discoveryYear: 1969, orbitalPeriod: 1327.3582 },
    { name: '5 Astraea', discoveryYear: 1845, orbitalPeriod: 1507.279 },
    { name: '5145 Pholus', discoveryYear: 1992, orbitalPeriod: 33711 },
    { name: '4769 Castalia', discoveryYear: 1989, orbitalPeriod: 400.428 },
    { name: '624 Hektor', discoveryYear: 1907, orbitalPeriod: 4358.521 },
    { name: '216 Kleopatra', discoveryYear: 1880, orbitalPeriod: 1707 },
    { name: '3753 Cruithne', discoveryYear: 1986, orbitalPeriod: 364.019 },
    { name: '3 Juno', discoveryYear: 1804, orbitalPeriod: 1593.926 },
    { name: '10 Hygiea', discoveryYear: 1849, orbitalPeriod: 2029.776 },
    { name: '21 Lutetia', discoveryYear: 1852, orbitalPeriod: 1387.902 },
    { name: '253 Mathilde', discoveryYear: 1885, orbitalPeriod: 1572.491 },
    { name: '7 Iris', discoveryYear: 1847, orbitalPeriod: 1346.628 },
    { name: '433 Eros', discoveryYear: 1898, orbitalPeriod: 643.219 },
    { name: '90377 Sedna', discoveryYear: 2003, orbitalPeriod: 4154395 },
    { name: '10199 Chariklo', discoveryYear: 1997, orbitalPeriod: 23084 },
    { name: '87 Sylvia', discoveryYear: 1866, orbitalPeriod: 2381.639 },
    { name: '90482 Orcus', discoveryYear: 2004, orbitalPeriod: 89606 },
    { name: '5335 Damocles', discoveryYear: 1991, orbitalPeriod: 14880.9 },
    { name: '8 Flora', discoveryYear: 1847, orbitalPeriod: 1192.956 },
    { name: '2060 Chiron', discoveryYear: 1977, orbitalPeriod: 18429 },
    { name: '5261 Eureka', discoveryYear: 1990, orbitalPeriod: 686.829 },
    { name: '588 Achilles', discoveryYear: 1906, orbitalPeriod: 4320.803 },
    { name: '28978 Ixion', discoveryYear: 2001, orbitalPeriod: 90717 },
    { name: '9 Metis', discoveryYear: 1848, orbitalPeriod: 1346.815 },
    { name: '7066 Nessus', discoveryYear: 1993, orbitalPeriod: 44561 },
    { name: '25143 Itokawa', discoveryYear: 1998, orbitalPeriod: 556.38 },
    { name: '10370 Hylonome', discoveryYear: 1995, orbitalPeriod: 44561 },
    { name: '45 Eugenia', discoveryYear: 1857, orbitalPeriod: 1638.654 },
    { name: '(308933) 2006 SQ372', discoveryYear: 2006, orbitalPeriod: 11800000 },
    { name: '2 Pallas', discoveryYear: 1802, orbitalPeriod: 1685.927 },
    { name: '8405 Asbolus', discoveryYear: 1995, orbitalPeriod: 27796 },
    { name: '20000 Varuna', discoveryYear: 2000, orbitalPeriod: 103440 },
    { name: '4 Vesta', discoveryYear: 1807, orbitalPeriod: 1325.886 },
    { name: '243 Ida', discoveryYear: 1884, orbitalPeriod: 1767.564 },
    { name: '951 Gaspra', discoveryYear: 1916, orbitalPeriod: 1199.479 },
    { name: '15760 Albion', discoveryYear: 1992, orbitalPeriod: 105757 },
    { name: 'Arrokoth', discoveryYear: 2014, orbitalPeriod: 107847 },
    { name: '101955 Bennu', discoveryYear: 1999, orbitalPeriod: 436.604 }
  ],
};

I want to get the duplicates of the discoveryYear with filter map and reduce methods. I do not know what to write in the rest of the reduce method to get all of the duplicates that are the most. I think the most that exist is the year 1847. If I remove the reduce method and just run the filter and map method I end up getting an array of all the years from the prior Array.

CodePudding user response:

As @Daedalus has mentioned in a comment, you are using reduce incorrectly (perhaps filter and map as well). You could rewrite your function like this to obtain the desired result:

function getGreatestDiscoveryYear(data) {
  const occurrences = data.reduce((acc, curr) => {
    if (curr.discoveryYear in acc) {
      return { ...acc, [curr.discoveryYear]: acc[curr.discoveryYear]   1 }
    }
    return { ...acc, [curr.discoveryYear]: 1 }
  }, {})

  const maxOccurrences = Math.max(...Object.values(occurrences))

  return Object.entries(occurrences).filter(([_, value]) => value === maxOccurrences).map(([key]) => key)
}

First, you use reduce on data to get an object which has discoveryYear as keys and number of discoveries as values. Then, you need to find which year is the most prevalent - I used Math.max to achieve it. Finally, you can print an array of discovery years with the most occurrences (please note that there may be many of them).

CodePudding user response:

export function getGreatestDiscoveryYear(data) {
  const getGDY = data.asteroids;
  const results = getGDY.map(function (getDiscovers) {
    return getDiscovers;
  }).reduce(function (result, num) {
    result[num.discoveryYear] = (result[num.discoveryYear] || 0)   1;
    return result;
  });
  const addedResults = Object.keys(results).filter(function (getit) {
    return results[getit] > 2;
  });
  return parseInt(addedResults[0]);

This was the answer I was looking for. This gets me the result of the year as a number 1847.

  • Related