获取json数组

时间:2017-08-25 18:27:57

标签: javascript jquery

这是我的json ::

[{     “名字”:“罗伊”,     “城市”:“达拉斯”,     “州”:“德州” },{     “名字”:“卡拉”,     “城市”:“洛杉矶”,     “州”:“加州” },{     “名字”:“菲利克斯”,     “城市”:“拉斯维加斯”,     “州”:“内华达” },{     “名字”:“弗雷德”,     “城市”:“迈阿密”,     “州”:“佛罗里达州” },{     “名字”:“比尔”,     “城市”:“亚特兰大”,     “州”:“格鲁吉亚” },{     “名字”:“迈克”,     “城市”:“芝加哥”,     “州”:“伊利诺伊州” },{     “名字”:“蒂姆”,     “城市”:“伍斯特”,     “州”:“MA” },{     “名字”:“瑞恩”,     “城市”:“奥斯汀”,     “州”:“德州” },{     “名字”:“山姆”,     “城市”:“波士顿”,     “州”:“MA” },{     “名字”:“莎拉”,     “城市”:“休斯顿”,     “州”:“德州” }]

我希望捕获德克萨斯州的所有索引号:

请帮忙。

5 个答案:

答案 0 :(得分:1)

您可以使用map()filter()等方法。

var data = [{ "name": "Roy", "city": "Dallas", "state": "Texas" }, { "name": "Karla", "city": "LA", "state": "California" },{ "name": "Felix", "city": "Las Vegas", "state": "Nevada" },{ "name": "Fred", "city": "Miami", "state": "Florida" },{ "name": "Bill", "city": "Atlanta", "state": "Georgia" },{ "name": "Mike", "city": "Chicago", "state": "Illinois" },{ "name": "Tim", "city": "Worcester", "state": "MA" },{ "name": "Ryan", "city": "Austin", "state": "Texas" },{ "name": "Sam", "city": "Boston", "state": "MA" },{ "name": "Sarah", "city": "Houston", "state": "Texas" }];

var indexes = data.map(function(item, i){
    if(item.state == "Texas") return i;
}).filter(function(item){ return item!=undefined; });

console.log(indexes);

答案 1 :(得分:1)

您可以在收集匹配状态的索引时使用Array#reduce

var array = [{ name: "Roy", city: "Dallas", state: "Texas" }, { name: "Karla", city: "LA", state: "California" }, { name: "Felix", city: "Las Vegas", state: "Nevada" }, { name: "Fred", city: "Miami", state: "Florida" }, { name: "Bill", city: "Atlanta", state: "Georgia" }, { name: "Mike", city: "Chicago", state: "Illinois" }, { name: "Tim", city: "Worcester", state: "MA" }, { name: "Ryan", city: "Austin", state: "Texas" }, { name: "Sam", city: "Boston", state: "MA" }, { name: "Sarah", city: "Houston", state: "Texas" }],
    indices = array.reduce((r, o, i) => (o.state === 'Texas' && r.push(i), r), []);

console.log(indices);

答案 2 :(得分:0)

这是你需要的吗?

var obj=[{ "name": "Roy", "city": "Dallas", "state": "Texas" }, { "name": "Karla", "city": "LA", "state": "California" },{ "name": "Felix", "city": "Las Vegas", "state": "Nevada" },{ "name": "Fred", "city": "Miami", "state": "Florida" },{ "name": "Bill", "city": "Atlanta", "state": "Georgia" },{ "name": "Mike", "city": "Chicago", "state": "Illinois" },{ "name": "Tim", "city": "Worcester", "state": "MA" },{ "name": "Ryan", "city": "Austin", "state": "Texas" },{ "name": "Sam", "city": "Boston", "state": "MA" },{ "name": "Sarah", "city": "Houston", "state": "Texas" }],
indexes=[];

for(var index in obj){
  if(obj[index]['state']=='Texas')
    indexes.push(index);
}

console.log(indexes);

答案 3 :(得分:0)

您可以枚举集合并在数组中添加索引,其中state为“Texas”。如果使用服务器端代码,则可以使用JSON解析器对响应(对包含与JSON对象相同的属性的目标类型)进行反序列化。如果您使用的是Javascript,则可以将JSON.parse(jsonString)转换为对象数组,然后枚举

答案 4 :(得分:0)

您可以使用for循环来实现此目的。迭代一个变量,比如'i',会很有用。

texas_indecies = [];
for (var i = 0; i < array.length; i++){
  if (array[i].state == "Texas"){
    texas_indecies.push(i);
  }
}