Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
151
rated 0 times [  156] [ 5]  / answers: 1 / hits: 33812  / 10 Years ago, wed, february 26, 2014, 12:00:00

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 ?


More From » jquery

 Answers
3

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
});

[#72296] Tuesday, February 25, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
samsons

Total Points: 331
Total Questions: 97
Total Answers: 106

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
;