Converting a Js Object to an Array Using Jquery

Converting a JS object to an array using jQuery

var myObj = {
1: [1, 2, 3],
2: [4, 5, 6]
};

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

console.log(array);

Output:

[[1, 2, 3], [4, 5, 6]]

How to convert an Object to array using jquery

If I understand you correctly, your input is an array of objects, and your desired output is an array of arrays. This is easy with jQuery's $.map() method, but the only reason to use jQuery for this is if you're supporting IE < 9, because all modern browsers support the native Array.prototype.map() method:

var input = {

"countries": [

{ "country": "Australia", "employ_count": 22 },

{ "country": "Cameroon", "employ_count": 50 },

{ "country": "Germany", "employ_count": 13 }

]

};

var output = input.countries.map(function(v) {

return [v.country, v.employ_count];

});

var outputWithjQuery = $.map(input.countries, function(v) {

return [[v.country, v.employ_count]];

});

console.log(output);

console.log(outputWithjQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

How to convert Object into array in jquery/javascript

Use $.map()

var arr = $.map(Employee, function(value, key){
return value
})

Demo: Fiddle

Note: The order of loop is not dependable, so the order of values in the array may not be always same


Another way to handle it is to use a fixed array of keys so that the output array will have a predefined sequence

var keys = ['Name', 'ID', 'Address'];
var Employee = {};
Employee.Name = 'XYZ'
Employee.ID = 123
Employee.Address = 'ABC'
var arr = $.map(keys, function (key, idx) {
return Employee[key]
})
console.log(arr)

Demo: Fiddle

jQuery Convert Object Array to Array with keys

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).

Convert array of objects to an array of the object's values

If you need to do it using array here is another way .

Demo Code :

var response = [{
"id": 1,
"price": 20,
"name": "test"
}, {
"id": 4,
"price": 30,
"name": "test2"
}]
var outerarray = [];
$.each(response, function(key, val) {
innerarray = []
innerarray.push(val.id, val.price, val.name) //push value
outerarray.push(innerarray)
});
console.log(outerarray)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

JavaScript, transform object into array

Use Object.values it will return array.

Object.values(obj) // [24, 23, 33, 12, 31]

Convert an array of objects to another array of objects using jQuery

You are initializing the array incorrectly, and you need to replace the outer () by [], like

var results = [ {}, {} ];

You need to first make a map of different Key1's

var map = {};
results.forEach( function(item){
map[ item.Key1 ] = map[ item.Key1 ] || {};
map[ item.Key1 ][ item.Key3 ] = map[ item.Key1 ][ item.Key3 ] || 0;
map[ item.Key1 ][ item.Key3 ] += item.Key2;
})

Now iterate the keys of this map, and prepare your output

var output = Object.keys(map).map( function(value){
var obj = { key1 : value };
Object.keys( map[ value ] ).forEach( function(key2){
obj[ key2 ] = map[ value ][ key2 ]
});
return obj;
});

Demo

var results=[
{Key1:'Value1',Key2:100,Key3:'A'},
{Key1:'Value1',Key2:40,Key3:'B'},
{Key1:'Value2',Key2:60,Key3:'A'},
{Key1:'Value2',Key2:70,Key3:'B'},
{Key1:'Value3',Key2:50,Key3:'A'},
{Key1:'Value3',Key2:90,Key3:'B'}
];
var map = {};
results.forEach( function(item){
map[ item.Key1 ] = map[ item.Key1 ] || {};
map[ item.Key1 ][ item.Key3 ] = map[ item.Key1 ][ item.Key3 ] || 0;
map[ item.Key1 ][ item.Key3 ] += item.Key2;
});

var output = Object.keys(map).map( function(value){
var obj = { key1 : value };
Object.keys( map[ value ] ).forEach( function(key2){
obj[ key2 ] = map[ value ][ key2 ]
});
return obj;
});
console.log( output );

Convert object key-value pairs to a series of arrays in Javascript

You can map over the items to achieve this:

    s = {"Toothless":"Dragon","Foo":"Bar"};

var out = Object.keys(s).map(function(data){

return [data,s[data]];

});

console.log(out);

Converting JavaScript object with numeric keys into array

It's actually very straight forward with jQuery's $.map

var arr = $.map(obj, function(el) { return el });

FIDDLE

and almost as easy without jQuery as well, converting the keys to an array and then mapping back the values with Array.map

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

FIDDLE

That's assuming it's already parsed as a javascript object, and isn't actually JSON, which is a string format, in that case a run through JSON.parse would be necessary as well.

In ES2015 there's Object.values to the rescue, which makes this a breeze

var arr = Object.values(obj);


Related Topics



Leave a reply



Submit