JSON对象中的JavaScript查找属性 [英] JavaScript find property in JSON object

查看:453
本文介绍了JSON对象中的JavaScript查找属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的JSON对象:

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

问题出在那行:

var text = item[filter.field],

如果属性只是名称,则 item ['name'] 将适用于上述对象.但是如果我想得到描述, item ['details.descrption'] 不起作用. 因此,我需要一个允许我指定属性名称的函数,它将找到该属性并返回其值. 但是在我尝试写一个之前,我希望有人会遇到一个简单的解决方案.

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.

推荐答案

我通过创建以下函数解决了这个问题:

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

这篇关于JSON对象中的JavaScript查找属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆