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.

2
  • which keys wont you know exactly? Commented Nov 9, 2017 at 10:55
  • The "08", "07", "09". Commented Nov 9, 2017 at 15:27

2 Answers 2

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;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

That's perfect. Exactly what i wanted. Thanks a lot.
@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).
I'll give you an upvote anyway. It's a more elegant solution than mine I feel
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);
})

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.