1

I have an object array which looks like:

Object {0: "Ma. Jessa Martinez", 1: "Edwin Cuevas", 2: "Gerum Generol", 3: "Roy delos Reyes", 4: "Hannah Montecillo", 5: "Ralph Turla", 6: "Edralyn Danabar", 7: "Angelo Sto Domingo", 8: "Rhina Dela Cruz", 9: "Ricardo Camara", 10: "Joene Floresca"}

And I want to convert in array like:

[[0,"Ma. Jessa Martinez"],[1,"Edwin Cuevas"],[2,"Gerum Generol"], and so on]

I tried using

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

But it only return the values with no keys:

["Ma. Jessa Martinez", "Edwin Cuevas", "Gerum Generol", "Roy delos Reyes", "Hannah Montecillo", "Ralph Turla", "Edralyn Danabar", "Angelo Sto Domingo", "Rhina Dela Cruz", "Ricardo Camara", "Joene Floresca"]

Is there other way? I've search already in google I can't find a similar thing.

EDIT To be clear where my object array came from, I added this for reference. It came from an ajax request and already sorted:

var ticks = {};
$.each(result, function(key,value) {
    ticks[key] = value.name;
});
3
  • 2
    You dont need to build an array like [[0,"Ma. Jessa Martinez"],[1,"Edwin Cuevas"],[2,"Gerum Generol"], and so on]. You can already access your flat array with your_array[num_key] i.e. array[0] = "Ma. Jessa Martinez". Commented Mar 18, 2016 at 9:46
  • @PinkTurtle that's right, but in this question is wrong. Because an user can have another number instead 0, then it wouldn't work correctly. Commented Mar 18, 2016 at 9:50
  • Hi thanks guys. My data is correct and will always start from zero and is sorted. I will use it as a label in a Bar Graph Flot Charts and it requires the fortmat I mentioned above :) Thanks for the help I'll select Carlos's answer. Commented Mar 18, 2016 at 9:55

3 Answers 3

4

Use instead :

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

console.log(myobj_array);

@PinkTurtle point is important, because we may pay attention to the performance or use vanillajs instead jQuery.

However if the object structure use instead :

{80: "Ma. Jessa Martinez", 12: "Edwin Cuevas"}

and we process with only the index (and we retrieve it like arr[80] would be undefined, only if we use arr[0] would work, but the index of the user is not 0 , is 80).

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

3 Comments

Thanks Carlos, I already edited my question to explain where my object array came from. Feel free to check and if you could suggest a better way to do what I need. Thanks
@Carlos are you sure the extra brackets pair around [index,value] is correct/mandatory ?
he said he WANTS a structure like : [ [0,"Ma. Jessa Martinez"] ] (array that contains an array with indexes 0:id and 1:name). He can make a loop to process it when it's an object, but maybe he finds easily making it with arrays.
0

Or just use normal js:

var arr = [];
for (var i in obj) {
    if (obj.hasOwnProperty(i)) {
       arr.push([i, obj[i]]);
    }
}

Comments

0

You may create a new Javascript object and return as follow:

 var myobj_array= $.map(ticks, function(value, index) {
   Var obj=[[index,value]];
  return obj;
 });

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.