Rusher
Rusher

Reputation: 507

JSON: Get object by attribute

i have a json like this:

{
    "page": {
        "id": 220,
        "parentId": 196,
        "property": {
            "1": {
                "name": "h1",
                "value": "value i dont need",
            },
            "2": {
                "name": "tags",
                "value": "value i dont need",
            },
            "3": {
                "name": "description",
                "value": "value i need",
            },
}

The question is: how can i get "value i need", if the id of property ("3" in my case) is changing dynamicly? So, i cannot just use

data.page.property[3].value

btw, i'm using jquery.

thanks!

-- edited due to misunderstanding

Upvotes: 2

Views: 6099

Answers (7)

Lukas Liesis
Lukas Liesis

Reputation: 26413

Just wrote this general function which can take any object/array and find objects by attribute & it's value. No matter if you provide array, object or array of objects. No matter how deep structure is etc. It will just go through everything and find all objects which have defined attribute and it's value is equal to provided value. That's what i was searching when found this answer so hopefully somone will benefit:


function findObjectWithKey(obj, key, value, results) {
  if (obj && typeof obj === "object" && Array.isArray(obj)) {
    obj.forEach((_obj) => {
      results = findObjectWithKey(_obj, key, value, results);
    });
  } else if (obj && typeof obj === "object" && Object.keys(obj)?.length > 0) {
    Object.keys(obj).forEach((_objKey) => {
      if (_objKey === key && obj[_objKey] === value) {
        results.push(obj);
      }
      results = findObjectWithKey(obj[_objKey], key, value, results);
    });
  }
  return results;
}

Use:

const foundObjects = findObjectWithKey({some:{structure: false}}, 'structure', false, []);

Return: [{"structure":false}];

Upvotes: 1

PeterKA
PeterKA

Reputation: 24638

To get a specific value by name you can use a function such as the following:

function getValue( name ) {
    var val = '';
    $.each(data.page.property, function(k,v) { 
        if( v.name == name ) {
            val = v.value;
            return false;
        }
    });
    return val;
};

console.log( getValue( "description" ) ); //output: value i need
console.log( getValue( "tags" ) ); //output: value i dont need
console.log( getValue( "h1" ) ); //output: value i dont need

WORKING JSFIDDLE DEMO

Upvotes: 2

ebiv
ebiv

Reputation: 325

Hope the value you need is of the last member of the property object,

var len = Object.keys(json.page.property).length;
var myValue =json.page.property[len].value ;
console.log(myValue); // prints "value i need"

Upvotes: 0

Bhavik
Bhavik

Reputation: 4904

Pure Javascript Code

for (var k in json.page.property) {
    var o = json.page.property[k];
    if (o.value) alert(o.value); //Make sure it only alerts if there is value
}

Demo Fiddle

Upvotes: 0

Ted Xu
Ted Xu

Reputation: 1093

var prop = data.page.property
for(k in prop)
{
  if(prop[k].name == 'name you are interested')
    var v = prop[k].value || someDefaultValue; 
    // do something about v
}

Upvotes: 1

Bharath
Bharath

Reputation: 519

If its going to be a random object from some server, then only have is you have to iterate the object and compare what you need,

for(i in data.page.property)
{
     if(data.page.property[i].value && data.page.property[i].value === "your value")
        // do your stuff
}

Upvotes: 2

adjan
adjan

Reputation: 13674

Iterate over the property-array until you find a value field. then read it

Upvotes: 1

Related Questions