如何解析JS数组?

时间:2022-05-07 21:43:17

I have the following information:

我有以下信息:

  1. The city name, in this case Littelside. This is stored in a variable var city

    城市名称,在这种情况下Littelside。这存储在变量var city中

  2. I have an array of {} objects. Each contains address, city, name, state and zip-code

    我有一组{}对象。每个包含地址,城市,名称,州和邮政编码

[{"address":"07288 Albertha Station",**"city":"Littelside"**,"created_at":"2011-05-25T19:24:51Z","id":1,"name":"Mr. Emmitt Emmerich","state":"Missouri","updated_at":"2011-05-25T19:24:51Z","zip":"75475-9938"},{NEXT OBJECT WITH ADDRESS CITY ETC}]

[{“address”:“07288 Albertha Station”,**“city”:“Littelside”**,“created_at”:“2011-05-25T19:24:51Z”,“id”:1,“name”: “Emmitt Emmerich先生”,“州”:“Missouri”,“updated_at”:“2011-05-25T19:24:51Z”,“zip”:“75475-9938”},{下一个与地址城市等对象} ]

How do you use the var city as a search term to parse the array, and return address, city, state, name and zip of the array with the matching city?

如何使用var city作为解析数组的搜索词,并返回匹配城市的数组的地址,城市,州,名称和zip?

Thanks!

谢谢!

5 个解决方案

#1


1  

var myarr  =  [{"address":"07288 Albertha Station","city":"Littelside","created_at":"2011-05-25T19:24:51Z","id":1,"name":"Mr. Emmitt Emmerich","state":"Missouri","updated_at":"2011-05-25T19:24:51Z","zip":"75475-9938"}];
var city = 'Littelside';
$.each(myarr,function(key,value){
    if(value['city']== city){
        alert('hello');
    }
})

here's a working demo

这是一个有效的演示

#2


2  

The function you're looking for is $.grep().

您正在寻找的功能是$ .grep()。

var city='Littelside';

var cucc=$.grep(your_array, function (a) {
    return a.city==search;
})[0];

$.grep will still return an array, with all the elements inside that satisify the filter function. Here I've use [0] to get the first result, which is fine in case you're sure that there will be one result. So cucc will have the whole object you're looking for.

$ .grep仍然会返回一个数组,里面的所有元素都能满足过滤功能。在这里我使用[0]来获得第一个结果,如果您确定会有一个结果,这很好。所以cucc将拥有你正在寻找的整个对象。

jsFiddle Demo

jsFiddle演示

#3


1  

A function I use.

我使用的功能。

function arrayIndexOfProp(arr, prop, obj) {
        var i = arr.length;
        while (i--) {
            if (arr[i] && arr[i][prop] === obj) {
                return i;
            }
        }
        return -1;
    }

var index = arrayIndexOfProp(theArray, "city","Littelside");
// do stuff with theArray[index]

#4


1  

Array.prototype.reduce as in

Array.prototype.reduce如

Array.prototype.reduce(
    function (prev, current) {
      return prev || current.city === "Littelside" ? current : null;
    })

#5


-1  

Loop over the array until you find one where the_array[i].city matches.

循环遍历数组,直到找到the_array [i] .city匹配的地方。

#1


1  

var myarr  =  [{"address":"07288 Albertha Station","city":"Littelside","created_at":"2011-05-25T19:24:51Z","id":1,"name":"Mr. Emmitt Emmerich","state":"Missouri","updated_at":"2011-05-25T19:24:51Z","zip":"75475-9938"}];
var city = 'Littelside';
$.each(myarr,function(key,value){
    if(value['city']== city){
        alert('hello');
    }
})

here's a working demo

这是一个有效的演示

#2


2  

The function you're looking for is $.grep().

您正在寻找的功能是$ .grep()。

var city='Littelside';

var cucc=$.grep(your_array, function (a) {
    return a.city==search;
})[0];

$.grep will still return an array, with all the elements inside that satisify the filter function. Here I've use [0] to get the first result, which is fine in case you're sure that there will be one result. So cucc will have the whole object you're looking for.

$ .grep仍然会返回一个数组,里面的所有元素都能满足过滤功能。在这里我使用[0]来获得第一个结果,如果您确定会有一个结果,这很好。所以cucc将拥有你正在寻找的整个对象。

jsFiddle Demo

jsFiddle演示

#3


1  

A function I use.

我使用的功能。

function arrayIndexOfProp(arr, prop, obj) {
        var i = arr.length;
        while (i--) {
            if (arr[i] && arr[i][prop] === obj) {
                return i;
            }
        }
        return -1;
    }

var index = arrayIndexOfProp(theArray, "city","Littelside");
// do stuff with theArray[index]

#4


1  

Array.prototype.reduce as in

Array.prototype.reduce如

Array.prototype.reduce(
    function (prev, current) {
      return prev || current.city === "Littelside" ? current : null;
    })

#5


-1  

Loop over the array until you find one where the_array[i].city matches.

循环遍历数组,直到找到the_array [i] .city匹配的地方。