0

Current approach:

function stuff() {
   const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']
   const names ['tom', 'dave', 'kate', 'jane', 'james']

   const result = {}

   days.map(day => {
      const innerResult = {}

      names.map(name => {
         // some logic
         const res = {
            result: 'some result'
         }

         innerResult[name] = res
      })

      result[day] = innerResult
   })

   return result
}

which would look something like:

{
   "monday": {
      "tom": {
         result: 'some result'
      },
      "dave": {
         result: 'some result'
      },
      "kate": {
         result: 'some result'
      },
      "jane": {
         result: 'some result'
      },
      "james": {
         result: 'some result'
      },
   },
   "tuesday": {
      // ...etc
   }
}

is this possible to achieve without the use of mutable variables such as result and innerResult, can I return this structure straight from the map or something along those lines...

For instance, something like:

function stuff() {
   const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']
   const names ['tom', 'dave', 'kate', 'jane', 'james']

   return days.map(day => {
      return { [day]: names.map(name => {
         return {
            [name]: { result: 'some result' }
         }
      })}
   })
}

obviously the structure on those won't match but something along those lines, any help would be appreciated.

SkinnyBetas
  • 461
  • 1
  • 5
  • 27
  • NEVER use map if you do not need an array as output. Use forEach to just loop. In this case you could use reduce – mplungjan Sep 01 '22 at 07:21
  • Does this answer your question? "[Create an object from an array of keys and an array of values](/q/39127989/90527)", "[How create an object with keys which come from an array of keys without loops?](/q/67807806/90527)" – outis Sep 01 '22 at 07:55

2 Answers2

2

You could use Object.fromEntries() to create an object from an array of entries.

const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']
const names = ['tom', 'dave', 'kate', 'jane', 'james']

const output = Object.fromEntries(
    days.map(day => 
       [ 
        day, 
        Object.fromEntries( names.map(n => [n, { result: 'value' }]) ) 
       ]
     )
  )

console.log(output)

You could create an array of individual objects as in the questions. And then use Object.assign() to merge them into a single object

const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'],
      names = ['tom', 'dave', 'kate', 'jane', 'james'],
     
      // merges an array of objects into one
      merge = arr => Object.assign(...arr), 
      getNested = names => names.map(n => ({ [n]: {result: 'value' } }) ),
      output2 = merge( days.map(day => ({ [day]: merge(getNested(names)) })) )

console.log(output2)
adiga
  • 34,372
  • 9
  • 61
  • 83
2

Instead of map, you can use reduce

function stuff() {
   const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'];
   const names = ['tom', 'dave', 'kate', 'jane', 'james'];

    return days.reduce((previousValue, currentValue) => { 
        previousValue[currentValue] = names.reduce((previous, current) => {
            previous[current] = { result: 'some result' };
            return previous;
        }, {});

        return previousValue;
    }, {});    
}

console.log(stuff())
Sigh
  • 659
  • 13
  • 30