I have this simple JSON file (test.json):
我有这个简单的JSON文件(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"]
由于某些原因,我需要将每个“部分”值存储在一个单独的数组中,如下所示:[“bas-rhin”,“eure”]
I learned that $.makeArray()
can do the job, but didn't find out how. Here is my jQuery :
我了解到$ .makeArray()可以完成这项工作,但没有找到方法。这是我的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"]
.
使用该代码,我得到2个单独的数组:[“eure”]和[“bas-rhin”]。
Here is the question : How can I solve it and get these values in a single array ?
这是一个问题:如何解决它并在单个数组中获取这些值?
6 个解决方案
#1
5
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
});
#2
8
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
:
或者,使用jQuery的$ .map:
var arr = $.map(data.personnes, function (el) {
return el.departement;
});
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;
};
}
#3
1
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:
列出了从json对象创建数组的三种方法:
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 :
现在你看看图像:
#4
0
Try this
$( document ).ready(function() {
$.getJSON( "ajax/test.json", function( data ) {
console.log('loaded');
var departement_arr = [];
var departements = $.each(data.personnes, function (index, personne) {
departement_arr.push(personne.departement);
});
console.log(departement_arr);
});
});
#5
0
JavaScript does not have associative arrays; you can only push a value into an array. If you are looking for the following end result:
JavaScript没有关联数组;你只能将值推送到数组中。如果您正在寻找以下最终结果:
['abc','dg','erte']
// 0 1 2 <------- Index ( not 1 2 3 )
// 0 1 2 <-------索引(不是1 2 3)
use .each
var arr= new Array();
$.each( personnes, function(i, obj) {
arr.push( obj.personnes)
});
Or you could use .map
或者你可以使用.map
var arr = $.map( personnes, function( obj, i ) { return obj.personnes; } );
#6
0
Here is a way to do it using simple array logic:
以下是使用简单数组逻辑的方法:
$( document ).ready(function() {
$.getJSON( "ajax/test.json", function( data ) {
console.log('loaded');
var departements = [];
$.each(data.personnes, function (index, personne) {
departements[departements.length] = personne.departement;
});
console.log(arr);
});
});
#1
5
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
});
#2
8
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
:
或者,使用jQuery的$ .map:
var arr = $.map(data.personnes, function (el) {
return el.departement;
});
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;
};
}
#3
1
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:
列出了从json对象创建数组的三种方法:
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 :
现在你看看图像:
#4
0
Try this
$( document ).ready(function() {
$.getJSON( "ajax/test.json", function( data ) {
console.log('loaded');
var departement_arr = [];
var departements = $.each(data.personnes, function (index, personne) {
departement_arr.push(personne.departement);
});
console.log(departement_arr);
});
});
#5
0
JavaScript does not have associative arrays; you can only push a value into an array. If you are looking for the following end result:
JavaScript没有关联数组;你只能将值推送到数组中。如果您正在寻找以下最终结果:
['abc','dg','erte']
// 0 1 2 <------- Index ( not 1 2 3 )
// 0 1 2 <-------索引(不是1 2 3)
use .each
var arr= new Array();
$.each( personnes, function(i, obj) {
arr.push( obj.personnes)
});
Or you could use .map
或者你可以使用.map
var arr = $.map( personnes, function( obj, i ) { return obj.personnes; } );
#6
0
Here is a way to do it using simple array logic:
以下是使用简单数组逻辑的方法:
$( document ).ready(function() {
$.getJSON( "ajax/test.json", function( data ) {
console.log('loaded');
var departements = [];
$.each(data.personnes, function (index, personne) {
departements[departements.length] = personne.departement;
});
console.log(arr);
});
});