Ignore part of key in JSON call using jquery

I am currently filling out some json elements. However, if a new json is loaded. json looks a little different. How can I ignore the first part of the key so that it works. I thought I could use an asterisk, but this gives me an 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");

JSON Example 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"

   }
 }
 ]
+5
source share
8 answers

You can use a loop for:

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

http://jsfiddle.net/cSF5w/

+8
source

Assumption of objects:

var my1 = [{
    "attributes": {
        "edgebox.act.halt": "1",
            "edgebox.dyn.prop.current.profile": "1",
            "edgebox.dyn.prop.firmware": "1.0"
    }
}];
var my2 = [{
    "attributes": {
        "qln220.act.halt": "1",
            "qln220.dyn.prop.current.profile": "1",
            "qln220.dyn.prop.firmware": "1.0"
    }
}];

var items = [];

function pdata(data) {
    $.each(data, function (key, val) {
        items.push({mykey:val});
    });
}
pdata(my1[0].attributes);
pdata(my2[0].attributes);
alert(items[2].mykey);//alerts "1.0", 

in this example, an array of 6 objects

EDIT: using the same objects, but saving keys:

var items = [];

function pdata(data) {
    $.each(data, function (key, val) {
        items.push({mykey:val,origkey:key});
    });
}
pdata(my1[0].attributes);
pdata(my2[0].attributes);
alert(items[2].mykey+ ":"+items[2].origkey);// alerts "1.0:edgebox.dyn.prop.firmware"

EDIT2: split the first part:

function pdata(data) {
    $.each(data, function (key, val) {
        items.push({
            mykey: val,
            origkey:(key.substring(key.indexOf(".")+1))
        });
    });
}

fiddle for your pleasure: http://jsfiddle.net/ThXHd/1/

+2
var items = [];
for (key in data[0].attributes) {
    if (key.match('.stat.prop.type')) {
        items.push(data[0].attributes[key])
    }
}

displaySortLabel(items, "type-details");
+2

. . :

function getAttributesArray(obj, matchKey) {
   var items = [];
   for ( key in obj ) {
      if (key.match(matchKey)) {
          items.push(obj[key]);
      }
   }
   return items;
}

var label = getAttributesArray(data[0].attributes, '.dyn.prop.firmware')

displaySortLabel(label, "type-details");
+1

, '.' .

var newKey = key.slice(key.indexOf('.') + 1)

0

:

function getAttribute(attr, item) {
    var items = [];
    $.each(item.attributes, function (key, val) {
        if (key.slice(key.indexOf('.') + 1) == attr) 
            items.push(val);
    });
    return items;
};

:

getAttribute('dyn.prop.current.profile', data[0]);
0

, , @ undefined ( , ). .

:

function getOneWithSuffix(obj,suffix){
    for(var key in obj){
        if(key.substr(key.length-suffix.length)===suffix){
            return obj[key];
        }
    }
    return undefined;
}
// usage: myValue = getOneWithSuffix( data[0].attributes, '.dyn.prop.current.profile' )

:

function getManyWithSuffix(obj,suffix){
    var ret=[];
    for(var key in obj){
        if(key.substr(key.length-suffix.length)===suffix){
            ret.push(obj[key]);
        }
    }
    return ret;
}
// usage: myValuesArray = getManyWithSuffix( data[0].attributes, '.dyn.prop.current.profile' )

, .

0

, , - " " .

Here is what I have.

$.each(data[0].attributes, function(key, val){
     if(key.substr(key.indexOf('.',1)+1) === 'dyn.prop.current.profile')
         items.push(val);
});

I just said that some people already answered in this fiddle, hope this works: D

http://jsfiddle.net/n3jca/1/

0
source

All Articles