Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am beginner to AngularJS, I have data like below

{
    day1: 0,
    day2: 0,
    day3: 0,
    day4: 2
}

How can I convert these data into arrays like below?

[
    ["day1": 0],
    ["day2": 0],
    ["day3": 0],
    ["day4": 2]
]
share|improve this question
2  
The second format is invalid. After fixing it, you'd get pretty much the same thing as the first one. – Shomz Dec 17 '15 at 11:58
2  
Some years ago was "how I do X thing in jQuery", now this turned into "who I do X in Angular".... This is happening because people don't care about the language they're actually using, but just the tool to get things done right without worrying about the details – Matías Fidemraizer Dec 17 '15 at 12:02
var data = {day1: 0, day2: 0, day3: 0, day4: 2};
var dataArray = [];
angular.forEach(data, function(value, key) {
    dataArray.push([key, value]);
})

This will give you something along the lines of [["day1", 0], ["day2", 0], ["day3", 0], ["day4", 2]].

share|improve this answer

With plain Javascript:

var arr = Object.keys(obj).map(function(k) { return obj[k] });
share|improve this answer
    
This is not giving the desired result. – Bas Slagter Dec 17 '15 at 13:11

Not really related to AngularJS but you can do it like so (plain JS):

var myObject = {day1: 0, day2: 0, day3: 0, day4: 2};

var myArray = Object.keys(myObject).map(function(key) {
    var result = [];

    result[key] = myObject[key];  

    return result;
});
share|improve this answer
    
How come your using .call instead of just Object.keys().map. Ive only seen this used for map, forEach etc when you want to use it on array like objects. – ste2425 Dec 17 '15 at 12:02
1  
No reason actually...was in the heat of the moment :) – Bas Slagter Dec 17 '15 at 12:09

Using _.map in underscore.js

var objects = {day1: 0, day2: 0, day3: 0, day4: 2};
var arr = _.map(objects, function(obj) { return [obj] });
share|improve this answer
    
Are you suggesting to use a plugin just for doing this..?! there is no underscore tag in question. – T J Dec 17 '15 at 12:36
    
Yes. I would suggest. Underscore.js is a collection framework for javascript. If you want to use underscore function you need to include underscore.js in your script tag. More information about underscore.js pls look into underscorejs.org – Dinesh ML Dec 17 '15 at 12:39
    
I know very well what is underscore, the OP didn't mention he is using underscore. He didn't say he's open to using libraries either. As you can see, he wants to do it with plain javascript or angular.js framework. There is no need to import a whole library just to do a trivial task. – T J Dec 17 '15 at 12:44
    
I thought that If they use angular.js then they obviously use underscore for better and simple way of write the code, so I suggested and also he ll think of it in some how for the feature. So.. Is there any problem? – Dinesh ML Dec 17 '15 at 12:55
    
Yes, there is a problem because the question is not about solving the problem using _. So your answer does't answer the question. This is like I ask how to do x in java, someone answer hey I know it can be done using c++ like this, so you better use c++, anyway it's better than java. There is a reason why questions has a description and set of tags. This will be a valid option if OP is using Backbone.js, since _ is it's dependency. Angularjs has its own utility methods and doesn't depend on _ – T J Dec 17 '15 at 13:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.