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.

2

2 Answers 2

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)

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

Comments

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())

1 Comment

I would not use the word previousValue for the accumulator because it is not a previous value

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.