Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore part of the key in JSON call using jquery

Tags:

json

jquery

I'm currently populating some elements with json. However if a new json is loaded. the json looks slightly different. How can I ignore the first part of the key so that it works. I was thinking I could use an asterisk but that gives me a error in firebug.

Here is my jQuery

var items = [];
    $.each(data[0].attributes['*.dyn.prop.current.profile'], function (key, val) {
        items.push(val);
    });
    displaySortLabel(items, "type-details");

Example JSON 1

[
   {
    "avatarDefinitionId":1,
    "avatarName":"edge",
    "attributes":{
       "examplePrefixToSkipOver.act.halt":"1",
       "examplePrefixToSkipOver.dyn.prop.current.profile":"1",
       "examplePrefixToSkipOver.dyn.prop.firmware":"1.0",
       "examplePrefixToSkipOver.dyn.prop.ip.address.3g":"192.168.1.1",
       "examplePrefixToSkipOver.dyn.prop.ip.address.cloud.com":"192.168.1.1",
       "examplePrefixToSkipOver.dyn.prop.ip.address.lan":"10.0.0.1",
       "examplePrefixToSkipOver.dyn.prop.ip.address.wifi":"192.168.1.1",
       "examplePrefixToSkipOver.dyn.prop.location":"Chicago Office",
       "examplePrefixToSkipOver.dyn.prop.name":"examplePrefixToSkipOver",
       "examplePrefixToSkipOver.dyn.prop.priority":"1",
       "examplePrefixToSkipOver.dyn.prop.status.detail":"Placeholder-Text",
       "examplePrefixToSkipOver.dyn.prop.timestamp":"2010-01-01T12:00:00Z"

   }
 }
 ]
like image 762
ndesign11 Avatar asked Feb 14 '13 22:02

ndesign11


1 Answers

You can use a for loop:

for ( key in data[0].attributes ) {
    if (key.match('.dyn.prop.firmware')) {
       items.push(data[0].attributes[key]);
    }
}

http://jsfiddle.net/cSF5w/

like image 94
undefined Avatar answered Nov 14 '22 23:11

undefined