0

I have this json structure an can't find a way to access the data values(data1, data2 and date), i'd like to have those values in an array than i can sort by date:

{
"07" : {
  "07" : {
    "data1" : "-1",
    "data2" : "test",
    "date" : "1995-07-07"
  },
  "08" : {
    "data1" : "1",
    "data2" : "test",
    "date" : "1995-07-08"
  },
  "09" : {
    "data1" : "-1",
    "data2" : "test",
    "date" : "1995-07-09"
  },
  "10" : {
    "data1" : "-1",
    "data2" : "test",
    "date" : "1995-07-10"
  }
},
"08" : {
  "07" : {
    "data1" : "1",
    "data2" : "test",
    "date" : "1995-08-07"
  },
  "08" : {
    "data1" : "1",
    "data2" : "test",
    "date" : "1995-08-08"
  },
  "09" : {
    "data1" : "1",
    "data2" : "test",
    "date" : "1995-08-09"
  }
}
}

Because my keys aren't defined as constant i don't know what they'll be in advance.

loic13
  • 3
  • 2

2 Answers2

1

Polyfill for Object.entries:

const reduce = Function.bind.call(Function.call, Array.prototype.reduce);
const isEnumerable = Function.bind.call(Function.call, Object.prototype.propertyIsEnumerable);
const concat = Function.bind.call(Function.call, Array.prototype.concat);
const keys = Reflect.ownKeys;

if (!Object.values) {
    Object.values = function values(O) {
        return reduce(keys(O), (v, k) => concat(v, typeof k === 'string' && isEnumerable(O, k) ? [O[k]] : []), []);
    };
}

if (!Object.entries) {
    Object.entries = function entries(O) {
        return reduce(keys(O), (e, k) => concat(e, typeof k === 'string' && isEnumerable(O, k) ? [[k, O[k]]] : []), []);
    };
}

Code:

for (const [key, value] of Object.entries(myObject))
        {
            for (const [key2, value2] of Object.entries(value))
            {
                value2.data1;
                value2.data2;
                value2.date;
            }
        }

Instead Object.entries you can enumerate object like this.

for (var key in myObject)
{
    for (var key2 in myObject[key])
    {
        myObject[key][key2].data1;
        myObject[key][key2].data2;
        myObject[key][key2].date;
    }
}
Makla
  • 9,899
  • 16
  • 72
  • 142
  • That's perfect. Exactly what i wanted. Thanks a lot. – loic13 Nov 09 '17 at 14:19
  • @loic13 If this is true, then you should accept answer. Check icon at the top left of the answer. Since you are new on SO: you should also upvote ALL answers that helped you (up arrow). – Makla Nov 09 '17 at 14:32
  • 1
    I'll give you an upvote anyway. It's a more elegant solution than mine I feel – Plog Nov 09 '17 at 20:28
0

You can get all the key names from your json as an Array by calling the method:

keys = Object.getOwnPropertyNames(jsonObj);

In your example this will return an array ['07', '08'] to get the actual objects from the name you can call:

keys.forEach((key) => {
    objects = Object.getOwnPropertyDescriptor(jsonObj, key)
})

And then you can find the names of the keys of these objects and repeat

objects.forEach((object) => {
    keys = Object.getOwnPropertyNames(object);
})
Plog
  • 9,164
  • 5
  • 41
  • 66