深入搜索属性值后返回对象

时间:2019-06-14 15:20:59

标签: javascript

我有一个函数,可以在嵌套对象内部成功搜索属性值中包含的关键字,它返回嵌套对象,但是我希望它返回整个对象。

我正在搜索的数组如下:

var images = [
  {
    "node": {
      "comments_disabled": false,
      "edge_media_to_caption": {
        "edges": [
          {
            "node": {
              "text": "this text includes the hashtag #green"
            }
          }
        ]
      },
      "shortcode": "Byqehgyorxq"
    }
  },
  {
    "node": {
      "comments_disabled": false,
      "edge_media_to_caption": {
        "edges": [
          {
            "node": {
              "text": "this text includes the hashtag #red"
            }
          }
        ]
      },
      "shortcode": "Byqehgyorxq"
    }
  }
]

下面的代码将返回{“ text”:“此文本包含#红色的井号”}

function customFilter(object, result, key, value){
    if(object.hasOwnProperty(key) && object[key].includes(value))
        result.push(object);

    for(var i=0; i<Object.keys(object).length; i++){
        if(typeof object[Object.keys(object)[i]] == "object"){
            customFilter(object[Object.keys(object)[i]], result, key, value);
        }
    }
}
var result = []
customFilter(images, result, 'text', 'red');

我希望此函数返回带有所需输出的整个节点对象:

"node": { "comments_disabled": false, "edge_media_to_caption": { "edges": [ { "node": { "text": "this text includes the hashtag #red" } } ] }, "shortcode": "Byqehgyorxq"}

更新:修复了无效的对象

2 个答案:

答案 0 :(得分:3)

您可以通过查看对象的内部来找到对象。

function filter(array, key, value) {
    function find(object) {
        if (!object || typeof object !== 'object') return false;
        if (key in object && object[key].includes(value)) return true;
        return Object.values(object).some(find);
    }
    return array.find(find);
}

var images = [{ node: { comments_disabled: false, edge_media_to_caption: { edges: [{ node: { text: "this text includes the hashtag #red" } }] }, shortcode: "Byqehgyorxq" } }],
    result = filter(images, 'text', 'red');

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

您可以在数组上使用filter函数:

var images = [
  {
    "node": {
      "comments_disabled": false,
      "edge_media_to_caption": {
        "edges": [
          {
            "node": {
              "text": "this text includes the hashtag #green"
            }
          }
        ]
      },
      "shortcode": "Byqehgyorxq"
    }
  },
  {
    "node": {
      "comments_disabled": false,
      "edge_media_to_caption": {
        "edges": [
          {
            "node": {
              "text": "this text includes the hashtag #red"
            }
          }
        ]
      },
      "shortcode": "Byqehgyorxq"
    }
  }
]
                
function customFilter(object, key, value){
    var objectIsFound = false
    //if we found the desired object here we return true
    if(object.hasOwnProperty(key) && object[key].includes(value))
        return true
    //else we keep looking and if we found de desired combination we stop
    for(var i=0; i<Object.keys(object).length; i++){
        if(typeof object[Object.keys(object)[i]] == "object"){
           objectIsFound = customFilter(object[Object.keys(object)[i]], key, value);
           if (objectIsFound) break;
        }
    }
    return objectIsFound;
}

function imagesFilter(images, key, value) {
  //the .filter() will return an array of containing every object that returns true
  return images.filter(function(image){return customFilter(image, key, value)})
}

var result = imagesFilter(images, 'text', 'red');
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }