{
MainService: [
{
Id: "733",
Name: "service",
MainService: [
{
Id: "238",
Name: "service 1",
Service: [
{
Id: "145",
Name: "Service 2"
}
]
}
]
}
]
}
Id: "145",
Name: "Service 2"
function recur(branch, str) {
var str = str || '',
item;
for (item in branch) {
console.log(item)
if (typeof(branch[item]) === 'object') {
str += item;
str = recur(branch[item], str);
} else {
str += item;
}
}
return str;
}
var isArray = Array.isArray || function (arr) {
return Object.prototype.toString.call(arr) === "[object Array]";
}
var isObject = function (obj) {
return Object.prototype.toString.call(obj) === "[object Object]";
}
function three (tr) {
var paths = [];
function findPath(branch, str) {
Object.keys(branch).forEach(function (key) {
if (isArray(branch[key]) || isObject(branch[key]))
findPath(branch[key], str ? str + "." + key : key);
else
paths.push(str ? str + "." + key : key);
});
}
findPath(tr, "");
return paths;
}
[
"MainService.0.Id",
"MainService.0.Name",
"MainService.0.MainService.0.Id",
"MainService.0.MainService.0.Name",
"MainService.0.MainService.0.Service.0.Id",
"MainService.0.MainService.0.Service.0.Name"
]
var obj = {
MainService: [
{
Id: "733",
Name: "service",
MainService: [
{
Id: "238",
Name: "service 1",
Service: [
{
Id: "145",
Name: "Service 2"
}
]
}
]
}
]
};
function recur(branch, str_paths) {
var paths = str_paths.paths || [],
str = str_paths.str || '',
item;
for (item in branch) {
console.log(item)
if (typeof(branch[item]) === 'object' && branch[item] !== null) {
str += str.length ? '.' + item : item;
recur(branch[item], {str: str, paths: paths});
} else {
// str += '.' + item;
paths.push(str + '.' + item);
}
}
return paths;
}
var res = recur(obj, {});
console.log('Final res: ', res);
["MainService.0.Id",
"MainService.0.Name",
"MainService.0.MainService.0.Id",
"MainService.0.MainService.0.Name",
"MainService.0.MainService.0.Service.0.Id",
"MainService.0.MainService.0.Service.0.Name"]
var obj = {
MainService: [
{
Id: "733",
Name: "service",
MainService: [
{
Id: "238",
Name: "service 1",
Service: [
{
Id: "145",
Name: "Service 2"
}
]
}
]
}
]
};
function mapObject(obj,asArrays){
var paths=[];
function scan(obj,stack){
var k, v, path;
for(k in obj){
if(obj.hasOwnProperty(k)){
path=stack.concat([k]);
if((v=obj[k])!==null&&typeof v=='object'){
scan(v,path);
}else{
paths.push(asArrays?path:path.join('.'));
}
}
}
}
scan(obj,[]);
return paths;
}
console.warn(mapObject(obj));
console.warn(mapObject(obj,true)); //вдруг где пригодиться ;)