6

I have a data that the structure is like below:

var data = {
    0: {
        user: 1,
        job: "call center"
    },
    1: {
        user: 2,
        job: "programmer"
    }
}

Now I want to convert them to array of objects that looks like this:

[Object {user : 1, job  : call center}, {user : 2, job  : programmer} ]

Is it possible? How can I convert them. Any help, thanks.

2
  • Is it possible? yes Commented Dec 15, 2015 at 2:41
  • what should I need to do? Commented Dec 15, 2015 at 2:41

3 Answers 3

5

Try using map

 var array = $.map(data , function(value, index) {
     return [value];
 });

You can also do it without jQuery:

var array = Object.keys(data).map(function(k) { return obj[k] });
Sign up to request clarification or add additional context in comments.

6 Comments

FYI - There is no jQuery tag for this question.
Or wait, you are abusing jQuery's concat-map functionality (that is misnamed map) that can enumerate objects… Clever.
@RahulDesai what about now?
You don't really need the [] around value, and in fact you can just do $.map(data, Object)
The thing worth noting is that the Object.keys.map will be creating an array in the order the key is encountered, but the index/key of the object will not necessarily be the index in the array. So if an object has two keys (e.g., 0 and 3). A for loop would result in an array with 2 values in the corresponding positions and a bunch of undefineds in between (i.e., [{},undef,undef,{}]) . A map will just take the values and stick them in an array set ([{},{}]). There needs to be more clarification about the source and destination of the data.
|
2
Object.values(data)

yields

[{user: 1, job: 'call center'}, {user: 2, job: 'programmer'}]

Comments

1
  1. Your Object creation has poor syntax
  2. Below shows you how to do this with a simple for loop, but makes a lot of assumptions (e.g., your object keys are labeled correctly). I would ordinarily use map, but feel that may not be as easy/straightforward for you to understand; so the loop should be simple enough to follow.

var data =  { 0:{
                  user : 1,
                  job  : 'call center'
              },
              1:{
                  user : 2,
                  job  : 'programmer'
              }
            };


var arr  = [],
    keys = Object.keys(data);

for(var i=0,n=keys.length;i<n;i++){
  var key  = keys[i];
  arr[key] = data[key];
}

4 Comments

Crockford recommended to use forEach over traditional for loop.
@RahulDesai: Then ignore Crockford.
I like crockford, but think it's a matter of preference, whatever is easier to understand. The for..in may also be applicable, but it's difficult to determine what the OP's level is at. My default is to go to the traditional for, but yes foreach would also work. map is still preferred as it creates the array as a result of the iteration
Try adding a length property (m + 1) and run it through Array.prototype.slice.call(object). 99% less hacky and less work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.