0

I'm trying to get my head around map functions currently without much success.

I currently have a list of objects and am trying to remap to a new object using the map function to return a map indexed by the created property with a list of wooids.

Having written my map function, I only seem to be returning the last time.

const variants = [
{
   created: '2022-03-06',
    hashname: 'c78ba80402290724609a5e98c369c90984494152',
    hashobject: '80864e6329d5e305a512ace872ad7d56a3f41095',
    hashparent: '19c5d50ddddeb7c9a92469df78c47d9d611f1599',
    action: 'added',
    wooid: 7288
  },
{
   created: '2022-03-06',
    hashname: 'c78ba80402290724609a5e98c369c90984494152',
    hashobject: '80864e6329d5e305a512ace872ad7d56a3f41095',
    hashparent: '19c5d50ddddeb7c9a92469df78c47d9d611f1599',
    action: 'added',
    wooid: 7289
  }   
]

const res = Object.fromEntries(variants.map(k => [k.created, [k.wooid]]))
console.log(res)
Current output

{ '2022-03-06': [ 7289 ] }

Desired output:

{ '2022-03-06': [ 7289, 7288 ] }

Any help would be greatly appreciated as I'm unsure how to dynamically populate the array to include all other entires (6 in total).

8
  • Sure the code works - however not as I'm anticipating. I'm only getting back the last result - ideally I'm looking to return an object like so: { '2022-03-06': [ 7289, 7288 ] } Commented Mar 7, 2022 at 1:06
  • Sorry I've edited code to reflect Commented Mar 7, 2022 at 1:11
  • What determines the ordering in the resulting array? Why is 7289 first and not 7288 which appears first in the original array? Commented Mar 7, 2022 at 1:14
  • Nothing determines the ordering - I've just added to illustrate it capturing multiple values. Commented Mar 7, 2022 at 1:15
  • Ordering in arrays is significant so which order should the values be in? Commented Mar 7, 2022 at 1:15

1 Answer 1

1

Use Array.prototype.reduce() to build up your object. Check if you already have a key for each created value and if not, create an empty array. Then append the wooid value

const variants = [{"created":"2022-03-06","hashname":"c78ba80402290724609a5e98c369c90984494152","hashobject":"80864e6329d5e305a512ace872ad7d56a3f41095","hashparent":"19c5d50ddddeb7c9a92469df78c47d9d611f1599","action":"added","wooid":7288},{"created":"2022-03-06","hashname":"c78ba80402290724609a5e98c369c90984494152","hashobject":"80864e6329d5e305a512ace872ad7d56a3f41095","hashparent":"19c5d50ddddeb7c9a92469df78c47d9d611f1599","action":"added","wooid":7289}]

const res = variants.reduce((acc, { created, wooid }) => ({
  ...acc,
  [ created ]: [
    ...acc[created] ?? [], // init to an empty array if unset
    wooid
  ]
}), {})

console.log(res)

This will collect wooid values by created in the order they appear in the original data.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah ok - so reduce is the better option in this instance - thanks for your help!
@user1387363 Object.fromEntries() is just a simplified reduce anyway. You need something like the above in order to track existing created keys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.