2

Having the next object:

2018: {
    01: {
         01: { key: value }
         02: { key: value }
         03: { key: value }
        },
    02: { ...  }
    },
2019: {
    01: ...

How can I simplify the next code used for getting the values into each day object?

   for (let yearIndex in obj) {
      const year = obj[yearIndex]
      for (let monthIndex in year) {
        const month = year[monthIndex]
        for (let dayIndex in month) {
          const day = month[dayIndex] {
              console.log(day)
          }
        }
      }
    }

The ideal result may be an array with the object of each day:

[{ key: value }, { key: value }, ...]
10
  • 1
    I think this question is more suited for codereview.stackexchange.com Commented May 15, 2018 at 21:06
  • Object.entries(obj).map(([yearIndex, year]) => Object.entries(year).map(([monthIndex, month]) => ... could also simply further with Object.values if you don't care about indicies. Commented May 15, 2018 at 21:06
  • @mhodges asking how to shorten/simplify code is on topic for SO... Commented May 15, 2018 at 21:08
  • btw, what is response.val() retuning? Commented May 15, 2018 at 21:13
  • 1
    @JaredSmith Just said "more suited for". I didn't vote to close or downvote. Commented May 15, 2018 at 21:13

3 Answers 3

1

You could flat the objects by checking the type for a recursive call.

function getFlat(object) {
    return [].concat(
        ...Object
            .values(object)
            .map(value => value && typeof value === 'object' ? getFlat(value) : object)
    );
}

var object = {2018: { '01': { '01': { key: 1 }, '02': { key: 2 }, '03': { key: 3 } }, '02': { '01': { key: 4 }, '02': { key: 5 }, '03': { key: 6 } } }, 2019: { '01': { '01': { key: 7 }, '02': { key: 8 }, '03': { key: 9 } } } };

console.log(getFlat(object));

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

Comments

0

const obj = {
  2018: {
    01: {
      01: '01 first',
      02: '01 second',
    },
    02: {
      01: '02 first'
    }
  },
  2019: {
    01: {}
  }
};

Object.values(obj).forEach(year => {
  Object.values(year).forEach(month => {
    Object.values(month).forEach(day => {
      console.log(day);
    })
  })
})

Comments

0

If you are just trying to get the leaves of a tree (in other words, the values that are not themselves objects), you can make a simple recursive function. This works especially well when you don't know exactly how deep the nesting will be.

This will look at each value and if it's an object pass it back through. Otherwise it will push it to an array:

let obj = {2018: {'01': { 01: 'test18_11', 02: 'test18_12', 03: 'test18_13',},'02': { 01: 'test18_21', 02: 'test18_22', 03: 'test18_23',}},2019: {'01': { 01: 'test19_11', 02: 'test19_12', 03: 'test19_13', },'02': { 01: 'test19_21', 02: 'test19_22', 03: 'test19_23',}}}

function recurse(obj, a = []) {
    Object.values(obj).forEach(value => {
        if (typeof value === 'object') return recurse(value, a)
        else  a.push(value)
    })
    return a
}

console.log(recurse(obj))

Comments

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.