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

7
  • 1
    post some code dat u tried.. Commented Oct 18, 2013 at 16:03
  • 3
    FYI: This isn't JSON. It's a JavaScript array. JSON is a string representation of data that resembles JavaScript syntax. If it's not a string, it's not JSON. Commented Oct 18, 2013 at 16:04
  • payrollDefaults.State is not a value. payrollDefaults is an array, loop through each element, and then check it's .State value. Commented Oct 18, 2013 at 16:05
  • 1
    The code above shows a Javascript array. Also there's no such thing as a JSON object. benalman.com/news/2010/03/theres-no-such-thing-as-a-json Commented Oct 18, 2013 at 16:16
  • 1
    @UmarFarooqKhawaja Each array element is actually a JavaScript object, not JSON. JSON = JavaScript Object Notation. JSON is a format to use within a string or file which can then be parsed by the JSON object to become a JavaScript object. The term "JSON object" is generally reserved for the native JavaScript object containing the methods for parsing JSON and stringifying JS objects (i.e. JSON.parse() and JSON.stringify() respectively). Commented Oct 18, 2013 at 16:21

8 Answers 8

1

You could use this JS lib, DefiantJS (http://defiantjs.com) - which extends the global object JSON with the method "search". With this method, you can search a JSON structure with XPath expressions and it returns an array (empty if no matches were found).

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

Here is a working fiddle:
http://jsfiddle.net/hbi99/dMLdc/

Sign up to request clarification or add additional context in comments.

Comments

0

This should do the trick...

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

Also note that this loop only evaluates the length once, and not on each iteration, helping efficiency.

3 Comments

Anyone want to justify their downvote? This answer works and addresses the question.
Happened to mine too. Strange. Your answer is more thorough so definitely didn't deserve it, but I don't think even mine was misleading or incorrect enough to warrant downvoting!
@guypursey Happens all over the place on SO. I think there's downvote ninjas whose sole boring purpose in life is to discredit correct answers in an attempt to undermine the system!
0

You need to provide an index for the payrollDefaults array else your program won't know which specific object you want to pull the value of State from.

Just as an example, here's how to loop through all the State properties and print their values in the console.

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

Notice the [i] and how that refers to a specific object in your array?

Hope that helps make sense of what you were missing!

1 Comment

Bad answer? I thought this addressed the main part of the question which I took to be: "I've tried looping through payrollDefaults.State and I'm getting back nothing." I didn't provide a solution because I figured the OP already had that bit nailed and was missing something simple...
0
$.each(payrollDefaults,function(key,val){
  if(val.state == "NH"){
   console.log(val.MaintenancePercentage);
   console.log(val.OfficePercentage);
  }

});

This should help you

Comments

0

Using lodash.js's find

_.find(payrollDefaults, {State: 'NH'});
>>> {
        "State": "NH",
        "MaintenancePercentage": 21.45,
        "OfficePercentage": 22.56
    }

Comments

0

Try the below function:

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

    return result;
}

Comments

0

Here is how you need to search for a particular state

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

Here is a demo

Comments

0

Try this out:- http://jsfiddle.net/ba5ZQ/1/

JS:-

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

console.log(arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.