1

I need to iterate over javascript big object in memory so often for generate nested json data and insert into db. The object structure is like this:

var obj = {
    "30": { "7": { "date1": { "key1": "value1", "key2": "value2"}}},
    "29": { "8": { "date2": { "key1": "value1"}}},  
    "28": { "6": { "date3": { "key1": "value1", "key2": "value2", "key3": "value3"}}}   
};

actually I use this aproach for iterate over the object for change nesting levels

Object.keys(obj).map((number1) => {
    const number1Obj = obj[number1];
    Object.keys(number1Obj).map((number2) => {
        const number2Obj = number1Obj[number2];
        Object.keys(number2Obj).map((date) => {
            const dateObj = number2Obj[date];
            console.log(number1);
            console.log(number2);
            console.log(date);
            Object.keys(dateObj).map((key) => {
                console.log(`           ${key}:${dateObj[key]}`);
            });
        });
     });
  });

Is there a more efficient way of iterate over the object and its properties? running example

Borja Tur
  • 779
  • 1
  • 6
  • 20
  • 1
    Is this a performance bottleneck in your app, or is this premature optimization? It's better to have clear understandable code for the most part and focus your optimizations on the real issues later. – Krease Apr 15 '16 at 16:05
  • Thanks for the advice , currently It isn't a problem but I like to have thought my code in terms of efficiency. – Borja Tur Apr 15 '16 at 16:17
  • If you're worried about performance (but why are you?), then a `for` loop will always be faster. –  Apr 15 '16 at 17:23

1 Answers1

0

You might find this answer relevant, but not sure about performance issues.

Iterate through Nested JavaScript Objects

Community
  • 1
  • 1
ksliu25
  • 114
  • 1
  • 1
  • 6