0

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]
]
2
  • 2
    The second format is invalid. After fixing it, you'd get pretty much the same thing as the first one. Commented Dec 17, 2015 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 Commented Dec 17, 2015 at 12:02

4 Answers 4

3

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;
});
Sign up to request clarification or add additional context in comments.

4 Comments

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.
No reason actually...was in the heat of the moment :)
this returns an array with 0 length for each element, why?
It is supposed to return an array with 4 array's in it which each have one key with a value matching the input object. Like: [ [ day1: 0 ], [ day2: 0 ], [ day3: 0 ], [ day4: 2 ] ]
1
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]].

Comments

1

With plain Javascript:

var arr = Object.keys(obj).map(function(k) { return obj[k] });

1 Comment

This is not giving the desired result.
-1

Using _.map in underscore.js

var objects = {day1: 0, day2: 0, day3: 0, day4: 2};
var arr = _.map(objects, function(obj) { return [obj] });

8 Comments

Are you suggesting to use a plugin just for doing this..?! there is no underscore tag in question.
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
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.
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?
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 _
|

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.