0

I have a JSON object that has the following structure

{"30-31":[{"msg":"hello","usr":"31"}],
"33-30":[{"msg":"shah hi","usr":"30"}]}

What operation can I perform to get an array like this ["30-31", "33-30"]. I tried my best using .map etc but with no success, can anybody help?

0

2 Answers 2

5

There is a built-in Object.keys(obj) that you can use:

var json = {"30-31":[{"msg":"hello","usr":"31"}],
            "33-30":[{"msg":"shah hi","usr":"30"}]};
var keys = Object.keys(json);
console.log(keys);

Additionally, you can do it the hard (cross-browser) way with a for...in loop:

var json = {"30-31":[{"msg":"hello","usr":"31"}],
            "33-30":[{"msg":"shah hi","usr":"30"}]};
var keys = [];
for (k in json) keys.push(k);
console.log(keys);

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

Comments

5

you can use Object.keys()

let obj = {"30-31":[{"msg":"hello","usr":"31"}],
  "33-30":[{"msg":"shah hi","usr":"30"}]}
  
console.log(Object.keys(obj));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.