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]
]
T J
  • 42,762
  • 13
  • 83
  • 138
selvam
  • 1,177
  • 4
  • 18
  • 40
  • 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

4 Answers4

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;
});
Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
  • 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
  • this returns an array with 0 length for each element, why? – I Like Jan 17 '17 at 06:04
  • 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 ] ] – Bas Slagter Jan 17 '17 at 09:58
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]].

Fissio
  • 3,748
  • 16
  • 31
1

With plain Javascript:

var arr = Object.keys(obj).map(function(k) { return obj[k] });
Matias
  • 461
  • 1
  • 5
  • 21
-1

Using _.map in underscore.js

var objects = {day1: 0, day2: 0, day3: 0, day4: 2};
var arr = _.map(objects, function(obj) { return [obj] });
Dinesh ML
  • 921
  • 11
  • 15
  • 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 http://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
  • Yes of course the question is not about unserscore, its about "How to Convert single object into multiple array in angularjs" . So he wants the solution using angularjs then why you guys are suggesting plain javascript. Like that I can suggest to do the logic using underscore.js. This is not an offense. If they are often needs to do the logic like that , they can think of it in future. If they are open minded other wise just leave it or press down vote. – Dinesh ML Dec 17 '15 at 13:08
  • Because there is a `javascript` tag in question. Look at the tags below question. Even if it's not, angularjs is a javascript library, and it can't exist without javascript. But it can without underscore. FYI, "use jQuery" is a joke in Stackoverflow community. – T J Dec 17 '15 at 13:10
  • I can understand that point from here by you. I ll think of it in future to suggest something before. but till you not got my point right? Any way thanks for some Ideas which I got from you. Cheers! – Dinesh ML Dec 17 '15 at 13:18