1

I have a JSON object that looks a bit like this:

{
    name: 'test',
    details: {
        description: 'This is the long description',
        shortDescription: 'This is the short description (ironically longer than the description!)'
    }
}

Obviously the real object is a lot more complicated than this example, but I have omitted the details because they will only complicate the question. So, with this object, I have a function that tries to get the value of the property, it looks like this:

// Private function for matching fields
var _matchField = function (item, filter) {

    // Our variables
    var text = item[filter.field],
        values = filter.expression.split(',');

    // If we have any text
    if (text) {

        // Loop through our values
        angular.forEach(values, function (value) {

            console.log(text);
            console.log(value);

            // See if we have a match
            if (text.toLowerCase().indexOf(value.toLowerCase()) > -1) {

                // We have found a match
                return true;
            }
        });
    }

    // We have found no matches
    return false;
}

The issue is the line:

var text = item[filter.field],

If the property was just the name then item['name'] would work with the above object. But if I want to get the description; item['details.descrption'] doesn't work. So I need a function that will allow me to specify a property name and it will find the property and return its value. But before I try to write one, I was hoping there might be a simple solution that someone has come across.

3 Answers 3

1

you can write your custom function for this

function getProperty(json, field) {

   if (json == null || field == null) {
       return null;
   }

   var value = json;
   var fields = field.split(".");
   for (var i = 0; i < fields.length; i++) {

       value = value[fields[i]];

       if (value == null) {
           return null;
       }
   }

   return value;
}

check this plnkr example https://plnkr.co/edit/8Ayd9wnh1rJh1ycx5R1f?p=preview

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

Comments

0

You can split the reference to the object and use a function for getting the right nested object/value.

function getValue(o, p) {
    if (typeof p === 'string') {
        p = p.split('.')
    }
    return p.length ? getValue(o[p.shift()], p) : o;
}

var item = { name: 'test', details: { description: 'This is the long description', shortDescription: 'This is the short description (ironically longer than the description!)' } };

document.write(getValue(item, 'details.description'));

Comments

0

I solved this by creating this function:

// Private function to get the value of the property
var _getPropertyValue = function (object, notation) {

    // Get all the properties
    var properties = notation.split('.');

    // If we only have one property
    if (properties.length === 1) {

        // Return our value
        return object[properties];
    }

    // Loop through our properties
    for (var property in object) {

        // Make sure we are a property
        if (object.hasOwnProperty(property)) {

            // If we our property name is the same as our first property
            if (property === properties[0]) {

                // Remove the first item from our properties
                properties.splice(0, 1);

                // Create our new dot notation
                var dotNotation = properties.join('.');

                // Find the value of the new dot notation
                return _getPropertyValue(object[property], dotNotation);
            }
        }
    }
};

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.