Convert array to array of objects with reduce

  • A+
Category:Languages
optimizedRoute = ['Bengaluru', 'Salem', 'Erode', 'Tiruppur', 'Coimbatore']   result = [  { start: bengaluru, end: salem }, { start: salem, end: erode }, { start: erode, end: tiruppur }, { start: tiruppur, end: coimbatore }, ] 

I want to convert optimizedRoute to result. I want to do this with es6 reduce. here is what i tried,

 const r = optimizedRoute.reduce( (places, place, i) => {     const result: any  = [];     places = []     places.push({       startPlace: place,       endPlace: place     });     // result.push ({ startplace, endplace, seats: 4 });     // console.log(result);     return places;   }, {});   console.log(r) 

 


You could use reduce for getting start and end part of the route and return the end for the next start.

getParts = a => (                   // take a as array and return an IIFE     r => (                          // with an initialized result array         a.reduce((start, end) => (  // reduce array by taking two values             r.push({ start, end }), // push short hand properties             end                     // and take the last value as start value for next loop         )),         r                           // finally return result     ) )([]);                              // call IIFE with empty array 
const getParts = a => (r => (a.reduce((start, end) => (r.push({ start, end }), end)), r))([]);  var optimizedRoute = ['Bengaluru', 'Salem', 'Erode', 'Tiruppur', 'Coimbatore']  console.log(getParts(optimizedRoute));
.as-console-wrapper { max-height: 100% !important; top: 0; }


@EDIT Grégory NEUT adding explaination

// Two thing to know first :  // When no initial value is provided, // Array.reduce takes the index 0 as first value and start to loop at index 1  // Doing (x, y, z) // Will execute the code x, y and z  // Equivalent to :  // x; // y; // z;  let ex = 0;  console.log((ex = 2, ex = 5, ex = 3));  // So about the code  const getParts = (a) => {   // We are creating a new function here so we can have an array where to   // push data to   const func = (r) => {     // Because there is no initial value     //     // Start will be the value at index 0 of the array     // The loop is gonna start at index 1 of the array     a.reduce((start, end) => {       console.log(start, end);        r.push({         start,         end,       });        return end;     });      return r;   };    return func([]); };  // Equivalent const getPartsEquivalent = (a) => {   const r = [];    // Because there is no initial value   //   // Start will be the value at index 0 of the array   // The loop is gonna start at index 1 of the array   a.reduce((start, end) => {     console.log(start, end);      r.push({       start,       end,     });      return end;   });    return r; };  var optimizedRoute = ['Bengaluru', 'Salem', 'Erode', 'Tiruppur', 'Coimbatore']  console.log(getPartsEquivalent(optimizedRoute));
.as-console-wrapper {   max-height: 100% !important;   top: 0; }

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: