Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

JS - converts objects to array

I have following array of objects:

[{"CZ-PR":"1"},{"CZ-JC":"0"},{"CZ-JM":"0"},{"CZ-KA":"0"},{"CZ-VY":"0"},{"CZ-KR":"0"},{"CZ-LI":"0"},{"CZ-MO":"0"},{"CZ-OL":"0"},{"CZ-PA":"0"},{"CZ-PL":"0"},{"CZ-ST":"0"},{"CZ-US":"0"},{"CZ-ZL":"0"}]

I need to convert it to array of arrays, like this (need to pass it to Google Maps Geocharts constructor):

[["CZ-PR","1"],["CZ-JC","0"]]

I tried:

var arr = [];
for (var k in obj) arr.push([+k, obj[k]]);

Which gives me array for each letter... How can I convert my initial object to what I need?

EDIT: The format that is expected from Google geocharts is this:

var data = google.visualization.arrayToDataTable([
 ['Country', 'Popularity'],
 ['Germany', 200],
 ['United States', 300],
 ['Brazil', 400],
 ['Canada', 500],
 ['France', 600],
 ['RU', 700]
 ]);

Answer*

Draft saved
Draft discarded
Cancel
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Commented Mar 10, 2024 at 22:24

lang-js

AltStyle によって変換されたページ (->オリジナル) /