代码之家  ›  专栏  ›  技术社区  ›  Mallikarjun Hampannavar

如何获取具有指定文本/字符而不是键值的json值

  •  1
  • Mallikarjun Hampannavar  · 技术社区  · 9 年前

    我有一个奇怪的需求,需要逐个值从JSON对象中获取值,例如在下面的JSON对象中,如果我将值传递为 而不是像 史蒂夫·乔恩 ,如何循环整个json对象并在整个json对象中找到匹配的对象。 我的json对象非常大,请建议我任何好的方法来循环整个对象而不影响性能。我已经检查了JSONPath表达式,但它不起作用

    提前感谢

    grouped_people: {
        'friends': [
            {name: 'Steve Jon', country: 'NZ'},
            {name: 'Jane Ken', country: 'US'},
            {name: 'Mike Jhon', country: 'AU'},
            {name: 'Mary Mani', country: 'NZ'},
        ],
        'enemies': [
            {name: 'Evil Steve', country: 'AU'},
            {name: 'Betty', country: 'NZ'},
        ]
    }
    
    3 回复  |  直到 9 年前
        1
  •  0
  •   Thusitha    9 年前

    你可以使用 recursive 函数以查找 object 其中包含以下给定值。

    var grouped_people = {
      'friends': [{
          name: 'Steve Jon',
          country: 'NZ'
        },
        {
          name: 'Jane Ken',
          country: 'US'
        },
        {
          name: 'Mike Jhon',
          country: 'AU'
        },
        {
          name: 'Mary Mani',
          country: 'NZ'
        },
      ],
      'enemies': [{
          name: 'Evil steve',
          country: 'AU'
        },
        {
          name: 'Betty',
          country: 'NZ'
        },
      ]
    };
    
    //Recursive function to search text in the object
    function findObject(obj, text, callback) {
      for (var k in obj) {
        if (typeof obj[k] == "object" && obj[k] !== null) {
          findObject(obj[k], text, callback);
        } else {
          if (obj[k].toLowerCase().indexOf(text.toLowerCase()) !== -1) {
            callback(obj);            
          }
        }
      }
    }
    
    
    findObject(grouped_people, "Steve", function(obj) {
      //All matched objects will return here.
      console.log(obj);
    });
        2
  •  0
  •   n.y    9 年前

    我认为blow函数(来自谷歌的Angular项目)可能会帮助你:

    /* Seach in Object */
    
    var comparator = function(obj, text) {
    if (obj && text && typeof obj === 'object' && typeof text === 'object') {
        for (var objKey in obj) {
            if (objKey.charAt(0) !== '$' && hasOwnProperty.call(obj, objKey) &&
                    comparator(obj[objKey], text[objKey])) {
                return true;
            }
        }
        return false;
    }
    text = ('' + text).toLowerCase();
    return ('' + obj).toLowerCase().indexOf(text) > -1;
    };
    
    var search = function(obj, text) {
    if (typeof text == 'string' && text.charAt(0) === '!') {
        return !search(obj, text.substr(1));
    }
    switch (typeof obj) {
        case "boolean":
        case "number":
        case "string":
            return comparator(obj, text);
        case "object":
            switch (typeof text) {
                case "object":
                    return comparator(obj, text);
                default:
                    for (var objKey in obj) {
                        if (objKey.charAt(0) !== '$' && search(obj[objKey], text)) {
                            return true;
                        }
                    }
                    break;
            }
            return false;
        case "array":
            for (var i = 0; i < obj.length; i++) {
                if (search(obj[i], text)) {
                    return true;
                }
            }
            return false;
        default:
            return false;
    }
    };
    

    this post

        3
  •  0
  •   Emad Dehnavi    9 年前

    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
      for (var subKey in obj[key]) {        
        if(obj[key][subKey].name.toLowerCase().search(keyword)!=-1){
            alert('The keyword exist')
            return true;
        }
      }       
    
      }
        alert('not found');
        return false;   
    }
    

    我假设您通过表单传递关键字,例如,您可以将其传递给如下方法:

    <input type="text" id="keyword" placeholder="Enter Keyword" />
    <input type="button" onclick="find(keyword.value)" value="find" />
    

    https://jsfiddle.net/emilvr/9o8s2680/2/