0

I'm attempting to search through this json for a specific value.

I would like to search by the State value, such as 'NH' and get back the MaintenancePercentage and OfficePercentage.

I've tried looping through payrollDefaults.State and I'm getting back nothing.

var payrollDefaults =
    [
        {
            "State": "NH",
            "MaintenancePercentage": 21.45,
            "OfficePercentage": 22.56
        },
        {
            "State": "NC",
            "MaintenancePercentage": 21.79,
            "OfficePercentage": 22.41
        },
        {
            "State": "OH",
            "MaintenancePercentage": 21.33,
            "OfficePercentage": 22.25
        },
        {
            "State": "RI",
            "MaintenancePercentage": 23.04,
            "OfficePercentage": 23.22
        },
        {
            "State": "SC",
            "MaintenancePercentage": 21.31,
            "OfficePercentage": 31.33
        },
        {
            "State": "TX",
            "MaintenancePercentage": 35.6,
            "OfficePercentage": 24.48
        },
        {
            "State": "VA",
            "MaintenancePercentage": 32.32,
            "OfficePercentage": 30.47
        },
        {
            "State": "AL",
            "MaintenancePercentage": 13.33,
            "OfficePercentage": 12.42
        },
        {
            "State": "CA",
            "MaintenancePercentage": 49.37,
            "OfficePercentage": 59.67
        },
        {
            "State": "FL",
            "MaintenancePercentage": 34.62,
            "OfficePercentage": 31.06
        },
        {
            "State": "GA",
            "MaintenancePercentage": 35.55,
            "OfficePercentage": 30.29
        },
        {
            "State": "IN",
            "MaintenancePercentage": 33.45,
            "OfficePercentage": 30.69
        },
        {
            "State": "MA",
            "MaintenancePercentage": 32.9,
            "OfficePercentage": 29.34
        },
        {
            "State": "MI",
            "MaintenancePercentage": 34.95,
            "OfficePercentage": 29.46
        },
        {
            "State": "MT",
            "MaintenancePercentage": 35.91,
            "OfficePercentage": 30.02
        }
    ]

EDIT:

I ended up converting this from a string to JSON.

var json = $.parseJSON(payrollDefaults);

4

8 回答 8

1

您可以使用这个 JS 库 DefiantJS ( http://defiantjs.com ) - 它使用“搜索”方法扩展了全局对象 JSON。使用此方法,您可以使用 XPath 表达式搜索 JSON 结构并返回一个数组(如果未找到匹配项则为空)。

var payrollDefaults = [
        { "State": "NH", "MaintenancePercentage": 21.45, "OfficePercentage": 22.56 },
        { "State": "NC", "MaintenancePercentage": 21.79, "OfficePercentage": 22.41 },
        { "State": "OH", "MaintenancePercentage": 21.33, "OfficePercentage": 22.25 },
        { "State": "RI", "MaintenancePercentage": 23.04, "OfficePercentage": 23.22 },
        { "State": "SC", "MaintenancePercentage": 21.31, "OfficePercentage": 31.33 },
        { "State": "TX", "MaintenancePercentage": 35.6,  "OfficePercentage": 24.48 },
        { "State": "VA", "MaintenancePercentage": 32.32, "OfficePercentage": 30.47 },
        { "State": "AL", "MaintenancePercentage": 13.33, "OfficePercentage": 12.42 },
        { "State": "CA", "MaintenancePercentage": 49.37, "OfficePercentage": 59.67 },
        { "State": "FL", "MaintenancePercentage": 34.62, "OfficePercentage": 31.06 },
        { "State": "GA", "MaintenancePercentage": 35.55, "OfficePercentage": 30.29 },
        { "State": "IN", "MaintenancePercentage": 33.45, "OfficePercentage": 30.69 },
        { "State": "MA", "MaintenancePercentage": 32.9,  "OfficePercentage": 29.34 },
        { "State": "MI", "MaintenancePercentage": 34.95, "OfficePercentage": 29.46 },
        { "State": "MT", "MaintenancePercentage": 35.91, "OfficePercentage": 30.02 }
    ],
    res = JSON.search( payrollDefaults, '//*[State = "NH"]' );

console.log( res[0].MaintenancePercentage );
console.log( res[0].OfficePercentage );

这是一个工作小提琴:http:
//jsfiddle.net/hbi99/dMLdc/

于 2014-01-07T07:04:53.417 回答
0

这应该可以解决问题...

http://jsfiddle.net/6zn7H/

function findState(state) {
    for(var i =0, l = payrollDefaults.length; i < l; i++){
        var value = payrollDefaults[i];
        if(value.State === state){
            return value;
        }
    }
}

另请注意,此循环仅评估一次长度,而不是每次迭代,以提高效率。

于 2013-10-18T16:04:31.907 回答
0
$.each(payrollDefaults,function(key,val){
  if(val.state == "NH"){
   console.log(val.MaintenancePercentage);
   console.log(val.OfficePercentage);
  }

});

这应该可以帮助你

于 2013-10-18T16:04:53.767 回答
0

使用lodash.js的 find

_.find(payrollDefaults, {State: 'NH'});
>>> {
        "State": "NH",
        "MaintenancePercentage": 21.45,
        "OfficePercentage": 22.56
    }
于 2013-10-18T16:04:54.673 回答
0

试试下面的功能:

function findByState(givenState) {
    var result = [];
    for(p in payrollDefaults) {
        if (p.State === givenState) {
            result.push(p);
        }
    }

    return result;
}
于 2013-10-18T16:05:13.080 回答
0

这是您需要如何搜索特定状态的方法

function GetMaintainancePercentage(state) {
    for (var i = 0; i < payrollDefaults.length; i++) {
        if (payrollDefaults[i]["State"] == state) {
            alert('Maintainance Percenatge is ' + payrollDefaults[i]["MaintenancePercentage"]);
        }
    }
}

这是一个演示

于 2013-10-18T16:06:29.307 回答
0

试试这个:- http://jsfiddle.net/ba5ZQ/1/

JS:-

var arr = jQuery.grep(payrollDefaults, function( a ) {
  return a.State =="NH";
});

console.log(arr);
于 2013-10-18T16:11:09.733 回答
0

您需要为payrollDefaults数组提供索引,否则您的程序将不知道您要从中提取值的特定对象State

举个例子,下面是如何遍历所有State属性并在控制台中打印它们的值。

for (var i = 0, l = payrollDefaults.length; i < l; i += 1) {
    console.log(payrollDefaults[i].State);
}

注意[i]以及它如何引用数组中的特定对象?

希望这有助于理解你所缺少的东西!

于 2013-10-18T16:03:21.807 回答