Recursively Search in JSON or Javascript Object

前端 未结 5 759
甜味超标
甜味超标 2020-12-20 07:08

For example:

[{
    id:\'our-purpose\',
    title:\'Our Purpose\',
    slug:\'/our-purpose\',
    backgroundImage:\'images/bg-our-purpose.jpg\',
    showInNa         


        
5条回答
  •  心在旅途
    2020-12-20 08:05

    function matchId(id, json){
      if (!(json && "object" === typeof json)) { return; }
      if (json.id === id) { return json; }
      for (var x in json){
        if (Object.hasOwnProperty.call(json, x)) {
          var result = matchId(id, json[x]);
          if (result !== undefined) { return result; }
        }
      }
    }
    

提交回复
热议问题