How would you get a JSONPath to all child nodes in an array of JSON object?

纵饮孤独 提交于 2019-12-04 10:35:12

I wrote a custom code that gives us JSON path of all nodes as array

function toArray(obj, name) {
    var result = [];
    var passName;
    var tempArray = [];
    for (var prop in obj) {
        var value = obj[prop];
        if (typeof value === 'object') {
            if ($.isNumeric(prop)) {
                passName = name + "[" + prop + "]";
            } else {
                passName = name + "['" + prop + "']";
            }
            tempArray = toArray(value, passName);
            $.each(tempArray, function (key, value) {
                result.push(value);
            });

        } else {
            result.push(name + "['" + prop + "']");
        }
    }
    return result;
}

JS Fiddle

well , I think you have to loop through your entire json structure..

  for(var i=0; i<data.length; i++) {
     for(var j in data[i]){
       //and so on
      }
   }

Or simply you can create aleas arrary for every key where key will be the path of that value with "_" as separator like..

 var aleasArray = []; 
 for(var i=0; i<data.length; i++) {
     for(var j in data[i]){
      aleasArray[i + '_' + j] = data[i][j];  // if you want make it more generalize aleas array..
      }
   }

hope this will help you..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!