0

I have variable which has data from JSON as below:

 var dictionary = {
     "data": [{ "id": "0", "name": "ABC" }, { "id": "1", "name": "DEF" }],
     "images": [{ "id": "0", "name": "PQR" }, { "id": "1", "name": "xyz" }]
 };

I want to split above json data using jquery and get them stored in three variables col[0], col[1] i want this result:

var names = Object.getOwnPropertyNames(dictionary);
var col = [];
col[0] = [{
  "id": "0",
  "name": "ABC"
}, {
  "id": "1",
  "name": "DEF"
}]
col[1] = [{
  "id": "0",
  "name": "PQR"
}, {
  "id": "1",
  "name": "xyz"
}]

this my code :

for (var i = 0; i < dictionary.length; i++) {
  for (var key in dictionary[i]) {
    if (col.indexOf(key) === -1) {
      col.push(key);
    }
  }
}
2
  • iam part solve this problem when edit post is clean Commented Oct 9, 2017 at 13:12
  • Please share valid dictionary object. Commented Oct 9, 2017 at 13:12

1 Answer 1

1

You can use Object.values().

var dictionary = {
     "data": [{ "id": "0", "name": "ABC" }, { "id": "1", "name": "DEF" }],
     "images": [{ "id": "0", "name": "PQR" }, { "id": "1", "name": "xyz" }]
 };
 
var col = Object.values(dictionary);
console.log(col);

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.