There is an array, inside an array of 7 hashes. How to take a value from hashes and put it into a separate array?

  • 0
    There is an array that is passed to the function as an argument:
    let city = [
            {c: 'Nashville', state: 'TN', latitudes: 36.17, longitudes: -86.78},
            {c: 'New York', state: 'NY', latitudes: 40.71, longitudes: -74.00},
            {c: 'Atlanta', state: 'GA', latitudes:33.75, longitudes: -84.39},
            {c: 'Denver', state: 'CO', latitudes: 39.74, longitudes: -104.98},
            {c: 'Seattle', state: 'WA', latitudes: 47.61, longitudes: -122.33},
            {c: 'Los Angeles', state: 'CA', latitudes: 34.05, longitudes: -118.24},
            {c: 'Memphis', state: 'TN', latitudes: 35.15, longitudes: -90.05},
        ];


    how to make 4 arrays in this function, which will contain:
    first array: all values ​​with;
    second array: all state values;
    third array: all latitudes;
    fourth array: all longitudes values;
    JavaScript Eliot Harmon, Feb 2, 2020

  • 1 Answers
  • 0
    const xxx = arr =>
    arr.reduce((acc, n) => (
    Object
    .entries(n)
    .forEach(([ k, v ]) => (acc[k] ||= []).push(v)),
    acc
    ), {});
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!