Make an Array from JSON object values with jQuery

33,471

Solution 1

I think you should try like this:

$.getJSON( "ajax/test.json", function( data ) {
    console.log('loaded');
    var departement = []; // create array here
    $.each(data.personnes, function (index, personne) {
        departement.push(personne.departement); //push values here
    });
    console.log(departement); // see the output here
});

Solution 2

Use map. It's much simpler:

var arr = data.personnes.map(function (el) {
  return el.departement;
});

console.log(arr); // ["bas-rhin", "eure"]

Alternatively, using jQuery's $.map:

var arr = $.map(data.personnes, function (el) {
  return el.departement;
});

Fiddle

If you need a polyfill for map:

if (!('map' in Array.prototype)) {
  Array.prototype.map = function (mapper, that /*opt*/) {
    var other = new Array(this.length);
    for (var i = 0, n = this.length; i < n; i++) {
      if (i in this) { other[i] = mapper.call(that, this[i], i, this); }
    }
    return other;
  };
}

Solution 3

There are many to declare a array. choose any one:

var arr = $.makeArray();
or
var arr = [];
or
var arr = new Array();

There are listed three way to create array from json object:

1.

$.each(data.personnes, function (index, personne) {
    arr[index] = personne.departement;
});

2.

var arr = personnes.personnes.map(function (element) {
    return element.departement;
});

3.

for(var index = 0;  index < data.personnes.length; index++) {
    arr[arr.length] = data.personnes[index].departement;
}

Now you have look on image :

enter image description here

Share:
33,471
notjb
Author by

notjb

Updated on February 27, 2020

Comments

  • notjb
    notjb about 4 years

    I have this simple JSON file (test.json):

    {"personnes":[
                {
                    "name":"Super",
                    "firstname":"Mario",
                    "adresse":["45 rue du poirier","6700","Strasbourg"],
                    "departement": "bas-rhin",
                },
                {
                    "name":"Super",
                    "firstname":"Luigi",
                    "adresse":["10 rue du muguet","6700","Strasbourg"],
                    "departement": "eure",
                }
    ]}
    

    For some reasons, I need to get each "departement" values to be stored in a single array like this :["bas-rhin","eure"]

    I learned that $.makeArray() can do the job, but didn't find out how. Here is my jQuery :

    $( document ).ready(function() {
        $.getJSON( "ajax/test.json", function( data ) {
            console.log('loaded');
            var departement;
            var departements = $.each(data.personnes, function (index, personne) {
                departement = personne.departement;
                var arr = $.makeArray(departement);
                console.log(arr)
            });
        });
    });
    

    With that code, I get 2 seperate arrays : ["eure"] and ["bas-rhin"].

    Here is the question : How can I solve it and get these values in a single array ?

  • Jai
    Jai about 10 years
    Its fine but ie8 is having issues with .map().
  • jmsg1990
    jmsg1990 about 10 years
    You could use $.map for a more compatible solution.