0

I have the variables in place and onclick function set up but I can't figure out how to turn this object into an array within an array. I'm trying to turn this

{"Wins":30,"Losses:"32}

into this:

[["Wins:",30]["Losses:",32]]

So I can append it to the data.addRows:

      var data = new google.visualization.DataTable();
        data.addColumn('string', 'Topping');
        data.addColumn('number', 'Total: ');
        data.addRows([
          ['String', number],
          ['String', number],
        ]);
4
  • 1
    You're looking for Object.entries Commented Mar 9, 2017 at 19:49
  • 3
    Is there a specific reason the keys contain a colon? Commented Mar 9, 2017 at 19:52
  • @IsaacWIvins Are you sure, you don't mean [["Wins:",30], ["Losses:",32]]. And why keep the : in "Wins:"? Commented Mar 9, 2017 at 19:55
  • when i console log westTeamData = JSON.stringify({"Wins": westTeamWins, "Losses": westTeamLosses}) is shows up as {"Wins":30,"Losses":32} I'm trying to have the westTeamData variable converted to an array within an array so I can append it to data.addRows(). Using the object.entries(westTeamData) im unfortunately returning an array of each individual character. Commented Mar 9, 2017 at 20:57

5 Answers 5

1

In the latest releases of some browsers, you can use the new Object.entries to do this:

(Credit to georg for mentioning this first in the comments above.)

var data = {
  "Wins": 30,
  "Losses": 32
}

console.log(
  Object.entries(data) //=> [ ['Wins', 30], ['Losses', 32] ]
)

However, you can also use the better-supported Object.keys and Array#map to create your own entries function:

var data = {
  "Wins": 30,
  "Losses": 32
}

function entries (object) {
  return Object.keys(data).map(function (k) { return [k, this[k]] }, data)
}

console.log(
  entries(data) //=> [ ['Wins', 30], ['Losses', 32] ]
)

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

3 Comments

If you credited Georg for his solution about Object.entries(), keep it up and credit me aswell, for Object.keys() solution.
@Kinduser I did not use your answer as inspiration. Pretty sure I was working on it when you posted yours; I came up with the answer independently.
Ah, fine then (:
1

I suggest you to use following approach:

var obj = {"Wins": 30, "Losses": 32};
    console.log(Object.keys(obj).map(v => [v + ':', obj[v]]));

4 Comments

And if the OP wants the : it would be [v + ':',obj[v]]
@epascarello It doesn't make any sense, I bet it's just a typo ;d Do you think that I should change it anyways?
@epascarello No need. We can be almost certain that the inclusion of those colons was a syntax mistake. If it wasn't a typo, there would have also been a colon after the closing quotation marks in the property keys.
@gyre Whatever, it's just nothing anyways. Lets give OP the solution he expects.
0

Object.entries() is great, but there is no IE support. Since you're using jQuery, you can se jQuery.map.

var myObj = {"Wins": 30,"Losses": 32};
var array = $.map(myObj, function(value, index) {
    return [[index, value]];
});
console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0

pure javascript

var myObject={"Wins":30,"Losses":32};
var resultArray=[];
for(var key in myObject) {
    if(myObject.hasOwnProperty(key)) {
        resultArray.push([key,myObject[key]])
    }
}
console.log(resultArray)

Result [["Wins", 30], ["Losses", 32]]

Comments

0

Following code stores each pair in an array:

var obj = { 'Wins': 30, 'Losses': 32 };
var keyvalues = [];
for (var prop in obj) {
    keyvalues.push([prop, obj[prop]]);
}

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.