1

Using TypeScript

Below is an array of objects & I want to map this in to a new object as provided below . (see the expected result)

// input array
const getPostAPI = 
[
  {
    get: '1234',
    post: 'abcd',
  },
  {
    get: '3423',
    post: 'dfcv',
  },
  {
    get: '1234',
    post: 'iucv',
  },
  {
    get: '1234',
    post: 'oipl',
  },
  {
    get: '3423',
    post: 'ffgf',
  },
  {
    get: '4567',
    post: 'tyui',
  },
  ]

from the above array of objects I want to map the post values as an array for repeating get values. Below I've provided the exptected result.

// output object
const exptectedResult = {
  '1234': ['abcd',
    'iucv',
    'oipl',
    '1234',],
  '3423': ['dfcv',
    'ffgf'],
  '4567': ['tyui']
  }

Following is what I've tried. But it is overwriting some of the values. i.e., I'm not getting the exact number of elements in the array of corresponding get key. (it is one less than actual)

this.getPostMap = this.getPostAPI.reduce(
  (map, api) => ({
    ...map,
    [api.get]: map[api.get]
      ? [...map[api.get], api.post]
      : [] || [],
  }),
  {}
);
mx_code
  • 2,441
  • 1
  • 12
  • 37
  • https://stackoverflow.com/a/40774906/9867451 – ibrahim mahrir Aug 12 '20 at 18:39
  • What you have is overwriting the values because if a `get` property already exist you don't push the current value in its array, you instead create a new array and assign it to the property. Using the same key in an object will result in one of them being discarded: `{ prop: "value 1", prop: "value 2" }` will yield `{ prop: "value 2" }` and `"value 1"`, which is in your case an array of `post` values, will be deleted – ibrahim mahrir Aug 12 '20 at 18:42
  • Can someone explain why mine is not working & this is working https://stackoverflow.com/a/40774906/9867451 – mx_code Aug 12 '20 at 18:46
  • [^^^ I just did](https://stackoverflow.com/questions/63382825/how-to-map-object-values-from-one-key-of-the-same-object-for-object-keys-with-sa#comment112077105_63382825) – ibrahim mahrir Aug 12 '20 at 18:46
  • ok. great. I missed that. – mx_code Aug 12 '20 at 18:47

3 Answers3

1

This simple piece of code will work very smoothly.

getPostAPI.reduce((acc, el)=>{
    (acc[el.get] = acc[el.get] || []).push(el.post)
    return acc
}, {})
Omkar Kulkarni
  • 1,091
  • 10
  • 22
1

That is quite a terrifying and unreadable block of code to do something that can be very simple. For example:

const getPostAPI = [{
    get: '1234',
    post: 'abcd',
  },
  {
    get: '3423',
    post: 'dfcv',
  },
  {
    get: '1234',
    post: 'iucv',
  },
  {
    get: '1234',
    post: 'oipl',
  },
  {
    get: '3423',
    post: 'ffgf',
  },
  {
    get: '4567',
    post: 'tyui',
  },
];

const expectedResult = getPostAPI.reduce((map, {get,post}) =>
  (map[get] = map[get] || []).push(post) && map,
{});

console.log(expectedResult);
Klaycon
  • 10,599
  • 18
  • 35
0

Your Problem is that when the get property is undefined, you actually want to fulfill it with the first post instead of an empty object :

this.getPostMap = this.getPostAPI.reduce(
  (map, api) => ({
    ...map,
    [api.get]: map[api.get]
      ? [...map[api.get], api.post]
      : [api.post],
  }),
  {}
);