1

I have an JSON obj:

 { 'A' : [1,2,3], 'B': [4,5,6], 'C': [7,8,9] }

I need to create multiple javascript arrays each to store

[1,2,3], [4,5,6], [7,8,9] 

separately. The JSON obj can have many such entires as it depends on what returns from SQL database, so the arrays has to be created dynamically. Please tell how to do this.

10
  • what's your expected output? Commented Feb 1, 2018 at 3:28
  • i think the expected output he want should be an double array. Commented Feb 1, 2018 at 3:30
  • 1
    @Dean,well,i mean output is three arrays or one array? [1,2,3], [4,5,6], [7,8,9] or [[1,2,3], [4,5,6], [7,8,9]] Commented Feb 1, 2018 at 3:36
  • 1
    @xianshenglu the arrays has to be created dynamically that he said Commented Feb 1, 2018 at 3:40
  • 2
    @ravikumar If you know exactly how many subarrays you will have in the multidimensional array, then you can use a destructuring assignment: const [one, two, three] = [[1,2,3], [4,5,6], [7,8,9]]; Commented Feb 1, 2018 at 4:44

2 Answers 2

4

✨ ES2017's Object.values()

If support is not a problem, this can't be more concise!

const object = { A: [1,2,3], B: [4,5,6], C: [7,8,9] };

console.log(Object.values(object));

🧓 ES5 Object.keys()

If you need better browser support, I would use Object.keys() together with Array.prototype.map() (ES5 if you change the arrow function for a normal function and the const for a var):

const object = { A: [1,2,3], B: [4,5,6], C: [7,8,9] };
  
console.log(Object.keys(object).map(key => object[key]));

📝 ES5 for...in

Otherwise, another approach with ES5 might be to use for...in. Longer to write, but probably faster (?)

var object = { A: [1,2,3], B: [4,5,6], C: [7,8,9] };
var listOfLists = [];

for (const key in object)
  if (object.hasOwnProperty(key))
    listOfLists.push(object[key]);
  
console.log(listOfLists);

🤔 Object.keys() VS for...in

As stated in the docs, the difference between Object.keys() and for...in is that the former will not consider properties in the prototype chain (so you don't need the object.hasOwnProperty(key) check).

The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

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

2 Comments

This pretty much works, but is there a way where i could say arr['A'] and output would be [1,2,3], arr['B'] and output would be [4,5,6] etc. like key-value pairs?
@ravikumar You can just use the object you had initially. In any of my examples, it would be just object.A or object['A'], and you will get [1, 2, 3]. If you have a JSON string representation of it instead of an actual Object, you can convert it just by doing: const object = JSON.parse('{ "A" : [1,2,3], "B": [4,5,6], "C": [7,8,9] }')
1

i hope this is what you want:

let obj = { 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] };
let result = Object.getOwnPropertyNames(obj).reduce(function(result, property) {
    result.push(obj[property]);
    return result;
}, []);
console.log(result);//[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]

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.