Sort-Index from nested JSON with Javascript












1















How can I recursively add a sort key to an infinite hierarchy like this:



[
{
"id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"name": "A",
"parent_id": null,
"sortNr": 1,
"children": [
{
"id": "07E556EE-F66F-49B5-B5E4-54AFC6A4DD9F",
"name": "A-C",
"parent_id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"sortNr": 3,
"children":
},
{
"id": "8C63981E-0D30-4244-94BE-658BAAF40EF3",
"name": "A-A",
"parent_id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"sortNr": 1,
"children": [
{
"id": "0BA32F23-A2CD-4488-8868-40AD5E0D3F09",
"name": "A-A-A",
"parent_id": "8C63981E-0D30-4244-94BE-658BAAF40EF3",
"sortNr": 1,
"children":
}
]
},
{
"id": "17A07D6E-462F-4983-B308-7D0F6ADC5328",
"name": "A-B",
"parent_id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"sortNr": 2,
"children":
}
]
},
{
"id": "64535599-13F1-474C-98D0-67337562A621",
"name": "B",
"parent_id": null,
"sortNr": 2,
"children":
},
{
"id": "1CE38295-B933-4457-BBAB-F1B4A4AFC828",
"name": "C",
"parent_id": null,
"sortNr": 3,
"children": [
{
"id": "D1E02274-33AA-476E-BA31-A4E60438C23F",
"name": "C-A",
"parent_id": "1CE38295-B933-4457-BBAB-F1B4A4AFC828",
"sortNr": 1,
"children": [
{
"id": "76A8259C-650D-482B-91CE-D69D379EB759",
"name": "C-A-A",
"parent_id": "D1E02274-33AA-476E-BA31-A4E60438C23F",
"sortNr": 1,
"children":
}
]
}
]
}
]


I want to get a sortable index.
For example 0000.0001.0003 or 0001.0003 for node A-C.



The function for leadingZeroes is



function fillZeroes (num) {
var result = ('0000'+num).slice(-4);
if (num===null){
return result
} else {
return '0000';
}
}


It should be sorted by sort number in each level of hierarchy, the sort number should be set newly every time, because I want to do rearrangement by setting it 1,5 to insert it between 1 and 2 (later for drag and drop capability). so 1;1,5;2 should become 1;2;3 and can then be translated to a sort-index like above.



I will also need it for indentation and breadcrumb-stuff.



How do I insert the proper sort-index to each object ?



The question is mainly about the recursion part. I am quite new to JavaScript



Thanks a lot










share|improve this question

























  • possible duplicate of stackoverflow.com/questions/26415675/…

    – Shoukat Mirza
    Nov 20 '18 at 12:09











  • Can you clarify about 1,5 - you mean that sortNr property could be floating point number?

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 12:29











  • @DaniilAndreyevichBaunov exactly. If there's just support for integers in JSON I would go with 2,4,6 as representations for 1,2,3 and "insert" with odd numbers.

    – nch68
    Nov 20 '18 at 12:35











  • @nch68, I see. So, to summarize, as I understand, you want to base your sort index on sortNr property. And also make it dense so that e.g. sortNr of 1, 5, 5.5, 6 within a single level will become something like 0001, 0002, 0003, 0004. Is it correct? Btw, the answer @georg has given is pretty need. Just needs some adjustment for your use case.

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 12:38













  • @DaniilAndreyevichBaunov exactly.

    – nch68
    Nov 20 '18 at 12:39
















1















How can I recursively add a sort key to an infinite hierarchy like this:



[
{
"id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"name": "A",
"parent_id": null,
"sortNr": 1,
"children": [
{
"id": "07E556EE-F66F-49B5-B5E4-54AFC6A4DD9F",
"name": "A-C",
"parent_id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"sortNr": 3,
"children":
},
{
"id": "8C63981E-0D30-4244-94BE-658BAAF40EF3",
"name": "A-A",
"parent_id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"sortNr": 1,
"children": [
{
"id": "0BA32F23-A2CD-4488-8868-40AD5E0D3F09",
"name": "A-A-A",
"parent_id": "8C63981E-0D30-4244-94BE-658BAAF40EF3",
"sortNr": 1,
"children":
}
]
},
{
"id": "17A07D6E-462F-4983-B308-7D0F6ADC5328",
"name": "A-B",
"parent_id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"sortNr": 2,
"children":
}
]
},
{
"id": "64535599-13F1-474C-98D0-67337562A621",
"name": "B",
"parent_id": null,
"sortNr": 2,
"children":
},
{
"id": "1CE38295-B933-4457-BBAB-F1B4A4AFC828",
"name": "C",
"parent_id": null,
"sortNr": 3,
"children": [
{
"id": "D1E02274-33AA-476E-BA31-A4E60438C23F",
"name": "C-A",
"parent_id": "1CE38295-B933-4457-BBAB-F1B4A4AFC828",
"sortNr": 1,
"children": [
{
"id": "76A8259C-650D-482B-91CE-D69D379EB759",
"name": "C-A-A",
"parent_id": "D1E02274-33AA-476E-BA31-A4E60438C23F",
"sortNr": 1,
"children":
}
]
}
]
}
]


I want to get a sortable index.
For example 0000.0001.0003 or 0001.0003 for node A-C.



The function for leadingZeroes is



function fillZeroes (num) {
var result = ('0000'+num).slice(-4);
if (num===null){
return result
} else {
return '0000';
}
}


It should be sorted by sort number in each level of hierarchy, the sort number should be set newly every time, because I want to do rearrangement by setting it 1,5 to insert it between 1 and 2 (later for drag and drop capability). so 1;1,5;2 should become 1;2;3 and can then be translated to a sort-index like above.



I will also need it for indentation and breadcrumb-stuff.



How do I insert the proper sort-index to each object ?



The question is mainly about the recursion part. I am quite new to JavaScript



Thanks a lot










share|improve this question

























  • possible duplicate of stackoverflow.com/questions/26415675/…

    – Shoukat Mirza
    Nov 20 '18 at 12:09











  • Can you clarify about 1,5 - you mean that sortNr property could be floating point number?

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 12:29











  • @DaniilAndreyevichBaunov exactly. If there's just support for integers in JSON I would go with 2,4,6 as representations for 1,2,3 and "insert" with odd numbers.

    – nch68
    Nov 20 '18 at 12:35











  • @nch68, I see. So, to summarize, as I understand, you want to base your sort index on sortNr property. And also make it dense so that e.g. sortNr of 1, 5, 5.5, 6 within a single level will become something like 0001, 0002, 0003, 0004. Is it correct? Btw, the answer @georg has given is pretty need. Just needs some adjustment for your use case.

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 12:38













  • @DaniilAndreyevichBaunov exactly.

    – nch68
    Nov 20 '18 at 12:39














1












1








1








How can I recursively add a sort key to an infinite hierarchy like this:



[
{
"id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"name": "A",
"parent_id": null,
"sortNr": 1,
"children": [
{
"id": "07E556EE-F66F-49B5-B5E4-54AFC6A4DD9F",
"name": "A-C",
"parent_id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"sortNr": 3,
"children":
},
{
"id": "8C63981E-0D30-4244-94BE-658BAAF40EF3",
"name": "A-A",
"parent_id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"sortNr": 1,
"children": [
{
"id": "0BA32F23-A2CD-4488-8868-40AD5E0D3F09",
"name": "A-A-A",
"parent_id": "8C63981E-0D30-4244-94BE-658BAAF40EF3",
"sortNr": 1,
"children":
}
]
},
{
"id": "17A07D6E-462F-4983-B308-7D0F6ADC5328",
"name": "A-B",
"parent_id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"sortNr": 2,
"children":
}
]
},
{
"id": "64535599-13F1-474C-98D0-67337562A621",
"name": "B",
"parent_id": null,
"sortNr": 2,
"children":
},
{
"id": "1CE38295-B933-4457-BBAB-F1B4A4AFC828",
"name": "C",
"parent_id": null,
"sortNr": 3,
"children": [
{
"id": "D1E02274-33AA-476E-BA31-A4E60438C23F",
"name": "C-A",
"parent_id": "1CE38295-B933-4457-BBAB-F1B4A4AFC828",
"sortNr": 1,
"children": [
{
"id": "76A8259C-650D-482B-91CE-D69D379EB759",
"name": "C-A-A",
"parent_id": "D1E02274-33AA-476E-BA31-A4E60438C23F",
"sortNr": 1,
"children":
}
]
}
]
}
]


I want to get a sortable index.
For example 0000.0001.0003 or 0001.0003 for node A-C.



The function for leadingZeroes is



function fillZeroes (num) {
var result = ('0000'+num).slice(-4);
if (num===null){
return result
} else {
return '0000';
}
}


It should be sorted by sort number in each level of hierarchy, the sort number should be set newly every time, because I want to do rearrangement by setting it 1,5 to insert it between 1 and 2 (later for drag and drop capability). so 1;1,5;2 should become 1;2;3 and can then be translated to a sort-index like above.



I will also need it for indentation and breadcrumb-stuff.



How do I insert the proper sort-index to each object ?



The question is mainly about the recursion part. I am quite new to JavaScript



Thanks a lot










share|improve this question
















How can I recursively add a sort key to an infinite hierarchy like this:



[
{
"id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"name": "A",
"parent_id": null,
"sortNr": 1,
"children": [
{
"id": "07E556EE-F66F-49B5-B5E4-54AFC6A4DD9F",
"name": "A-C",
"parent_id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"sortNr": 3,
"children":
},
{
"id": "8C63981E-0D30-4244-94BE-658BAAF40EF3",
"name": "A-A",
"parent_id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"sortNr": 1,
"children": [
{
"id": "0BA32F23-A2CD-4488-8868-40AD5E0D3F09",
"name": "A-A-A",
"parent_id": "8C63981E-0D30-4244-94BE-658BAAF40EF3",
"sortNr": 1,
"children":
}
]
},
{
"id": "17A07D6E-462F-4983-B308-7D0F6ADC5328",
"name": "A-B",
"parent_id": "D41F4D3D-EA9C-4A38-A504-4415086EFFF8",
"sortNr": 2,
"children":
}
]
},
{
"id": "64535599-13F1-474C-98D0-67337562A621",
"name": "B",
"parent_id": null,
"sortNr": 2,
"children":
},
{
"id": "1CE38295-B933-4457-BBAB-F1B4A4AFC828",
"name": "C",
"parent_id": null,
"sortNr": 3,
"children": [
{
"id": "D1E02274-33AA-476E-BA31-A4E60438C23F",
"name": "C-A",
"parent_id": "1CE38295-B933-4457-BBAB-F1B4A4AFC828",
"sortNr": 1,
"children": [
{
"id": "76A8259C-650D-482B-91CE-D69D379EB759",
"name": "C-A-A",
"parent_id": "D1E02274-33AA-476E-BA31-A4E60438C23F",
"sortNr": 1,
"children":
}
]
}
]
}
]


I want to get a sortable index.
For example 0000.0001.0003 or 0001.0003 for node A-C.



The function for leadingZeroes is



function fillZeroes (num) {
var result = ('0000'+num).slice(-4);
if (num===null){
return result
} else {
return '0000';
}
}


It should be sorted by sort number in each level of hierarchy, the sort number should be set newly every time, because I want to do rearrangement by setting it 1,5 to insert it between 1 and 2 (later for drag and drop capability). so 1;1,5;2 should become 1;2;3 and can then be translated to a sort-index like above.



I will also need it for indentation and breadcrumb-stuff.



How do I insert the proper sort-index to each object ?



The question is mainly about the recursion part. I am quite new to JavaScript



Thanks a lot







javascript json sorting nested






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 12:19







nch68

















asked Nov 20 '18 at 12:04









nch68nch68

63




63













  • possible duplicate of stackoverflow.com/questions/26415675/…

    – Shoukat Mirza
    Nov 20 '18 at 12:09











  • Can you clarify about 1,5 - you mean that sortNr property could be floating point number?

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 12:29











  • @DaniilAndreyevichBaunov exactly. If there's just support for integers in JSON I would go with 2,4,6 as representations for 1,2,3 and "insert" with odd numbers.

    – nch68
    Nov 20 '18 at 12:35











  • @nch68, I see. So, to summarize, as I understand, you want to base your sort index on sortNr property. And also make it dense so that e.g. sortNr of 1, 5, 5.5, 6 within a single level will become something like 0001, 0002, 0003, 0004. Is it correct? Btw, the answer @georg has given is pretty need. Just needs some adjustment for your use case.

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 12:38













  • @DaniilAndreyevichBaunov exactly.

    – nch68
    Nov 20 '18 at 12:39



















  • possible duplicate of stackoverflow.com/questions/26415675/…

    – Shoukat Mirza
    Nov 20 '18 at 12:09











  • Can you clarify about 1,5 - you mean that sortNr property could be floating point number?

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 12:29











  • @DaniilAndreyevichBaunov exactly. If there's just support for integers in JSON I would go with 2,4,6 as representations for 1,2,3 and "insert" with odd numbers.

    – nch68
    Nov 20 '18 at 12:35











  • @nch68, I see. So, to summarize, as I understand, you want to base your sort index on sortNr property. And also make it dense so that e.g. sortNr of 1, 5, 5.5, 6 within a single level will become something like 0001, 0002, 0003, 0004. Is it correct? Btw, the answer @georg has given is pretty need. Just needs some adjustment for your use case.

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 12:38













  • @DaniilAndreyevichBaunov exactly.

    – nch68
    Nov 20 '18 at 12:39

















possible duplicate of stackoverflow.com/questions/26415675/…

– Shoukat Mirza
Nov 20 '18 at 12:09





possible duplicate of stackoverflow.com/questions/26415675/…

– Shoukat Mirza
Nov 20 '18 at 12:09













Can you clarify about 1,5 - you mean that sortNr property could be floating point number?

– Daniil Andreyevich Baunov
Nov 20 '18 at 12:29





Can you clarify about 1,5 - you mean that sortNr property could be floating point number?

– Daniil Andreyevich Baunov
Nov 20 '18 at 12:29













@DaniilAndreyevichBaunov exactly. If there's just support for integers in JSON I would go with 2,4,6 as representations for 1,2,3 and "insert" with odd numbers.

– nch68
Nov 20 '18 at 12:35





@DaniilAndreyevichBaunov exactly. If there's just support for integers in JSON I would go with 2,4,6 as representations for 1,2,3 and "insert" with odd numbers.

– nch68
Nov 20 '18 at 12:35













@nch68, I see. So, to summarize, as I understand, you want to base your sort index on sortNr property. And also make it dense so that e.g. sortNr of 1, 5, 5.5, 6 within a single level will become something like 0001, 0002, 0003, 0004. Is it correct? Btw, the answer @georg has given is pretty need. Just needs some adjustment for your use case.

– Daniil Andreyevich Baunov
Nov 20 '18 at 12:38







@nch68, I see. So, to summarize, as I understand, you want to base your sort index on sortNr property. And also make it dense so that e.g. sortNr of 1, 5, 5.5, 6 within a single level will become something like 0001, 0002, 0003, 0004. Is it correct? Btw, the answer @georg has given is pretty need. Just needs some adjustment for your use case.

– Daniil Andreyevich Baunov
Nov 20 '18 at 12:38















@DaniilAndreyevichBaunov exactly.

– nch68
Nov 20 '18 at 12:39





@DaniilAndreyevichBaunov exactly.

– nch68
Nov 20 '18 at 12:39












2 Answers
2






active

oldest

votes


















1














Based on great answer by @georg. A bit adjusted solution based on sortNr object property.
You can run it straight as is with json being your object. The sort index is written into sortOrder property.



// Mutates the given object in-place.
// Assigns sortOrder property to each nested object
const indexJson = (json) => {

const obj = {children: json};

const format = (xs) => xs.map(x => pad(x, 4)).join('.');
const pad = (x, w) => (10 ** w + x).toString().slice(-w);

const renumber = (obj, path) => {
obj.path = path;
obj.sortOrder = format(path);
obj.children.slice()
.sort((obj1, obj2) => obj1.sortNr - obj2.sortNr)
.forEach((c, n) => renumber(c, path.concat(n+1)));
};

renumber(obj, );
};

indexJson(json);

console.log(JSON.stringify(json, null, 2));







share|improve this answer


























  • thanks a lot. this does the trick

    – nch68
    Nov 20 '18 at 21:29











  • @nch68, please upvote the answer if it is helpful. This works as a thanks here.

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 21:55











  • I can't because I have less than "15 reputation" :/

    – nch68
    Nov 20 '18 at 22:31













  • Oh, you are right :)

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 22:33



















1














Basically



let renumber = (obj, path) => {
obj.path = path
obj.children.forEach((c, n) => renumber(c, path.concat(n)))
}

renumber({children: yourData}, )


this creates a path property, which is an array of relative numbers. If you want to format it in a special way, then you can do



obj.path = format(path)


where format is like



let format = xs => xs.map(pad(4)).join(',')

let pad = w => x => (10 ** w + x).toString().slice(-w)





share|improve this answer


























  • thank you. I learned something today :)

    – nch68
    Nov 20 '18 at 21:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392644%2fsort-index-from-nested-json-with-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Based on great answer by @georg. A bit adjusted solution based on sortNr object property.
You can run it straight as is with json being your object. The sort index is written into sortOrder property.



// Mutates the given object in-place.
// Assigns sortOrder property to each nested object
const indexJson = (json) => {

const obj = {children: json};

const format = (xs) => xs.map(x => pad(x, 4)).join('.');
const pad = (x, w) => (10 ** w + x).toString().slice(-w);

const renumber = (obj, path) => {
obj.path = path;
obj.sortOrder = format(path);
obj.children.slice()
.sort((obj1, obj2) => obj1.sortNr - obj2.sortNr)
.forEach((c, n) => renumber(c, path.concat(n+1)));
};

renumber(obj, );
};

indexJson(json);

console.log(JSON.stringify(json, null, 2));







share|improve this answer


























  • thanks a lot. this does the trick

    – nch68
    Nov 20 '18 at 21:29











  • @nch68, please upvote the answer if it is helpful. This works as a thanks here.

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 21:55











  • I can't because I have less than "15 reputation" :/

    – nch68
    Nov 20 '18 at 22:31













  • Oh, you are right :)

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 22:33
















1














Based on great answer by @georg. A bit adjusted solution based on sortNr object property.
You can run it straight as is with json being your object. The sort index is written into sortOrder property.



// Mutates the given object in-place.
// Assigns sortOrder property to each nested object
const indexJson = (json) => {

const obj = {children: json};

const format = (xs) => xs.map(x => pad(x, 4)).join('.');
const pad = (x, w) => (10 ** w + x).toString().slice(-w);

const renumber = (obj, path) => {
obj.path = path;
obj.sortOrder = format(path);
obj.children.slice()
.sort((obj1, obj2) => obj1.sortNr - obj2.sortNr)
.forEach((c, n) => renumber(c, path.concat(n+1)));
};

renumber(obj, );
};

indexJson(json);

console.log(JSON.stringify(json, null, 2));







share|improve this answer


























  • thanks a lot. this does the trick

    – nch68
    Nov 20 '18 at 21:29











  • @nch68, please upvote the answer if it is helpful. This works as a thanks here.

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 21:55











  • I can't because I have less than "15 reputation" :/

    – nch68
    Nov 20 '18 at 22:31













  • Oh, you are right :)

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 22:33














1












1








1







Based on great answer by @georg. A bit adjusted solution based on sortNr object property.
You can run it straight as is with json being your object. The sort index is written into sortOrder property.



// Mutates the given object in-place.
// Assigns sortOrder property to each nested object
const indexJson = (json) => {

const obj = {children: json};

const format = (xs) => xs.map(x => pad(x, 4)).join('.');
const pad = (x, w) => (10 ** w + x).toString().slice(-w);

const renumber = (obj, path) => {
obj.path = path;
obj.sortOrder = format(path);
obj.children.slice()
.sort((obj1, obj2) => obj1.sortNr - obj2.sortNr)
.forEach((c, n) => renumber(c, path.concat(n+1)));
};

renumber(obj, );
};

indexJson(json);

console.log(JSON.stringify(json, null, 2));







share|improve this answer















Based on great answer by @georg. A bit adjusted solution based on sortNr object property.
You can run it straight as is with json being your object. The sort index is written into sortOrder property.



// Mutates the given object in-place.
// Assigns sortOrder property to each nested object
const indexJson = (json) => {

const obj = {children: json};

const format = (xs) => xs.map(x => pad(x, 4)).join('.');
const pad = (x, w) => (10 ** w + x).toString().slice(-w);

const renumber = (obj, path) => {
obj.path = path;
obj.sortOrder = format(path);
obj.children.slice()
.sort((obj1, obj2) => obj1.sortNr - obj2.sortNr)
.forEach((c, n) => renumber(c, path.concat(n+1)));
};

renumber(obj, );
};

indexJson(json);

console.log(JSON.stringify(json, null, 2));








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 13:18

























answered Nov 20 '18 at 13:02









Daniil Andreyevich BaunovDaniil Andreyevich Baunov

1,047528




1,047528













  • thanks a lot. this does the trick

    – nch68
    Nov 20 '18 at 21:29











  • @nch68, please upvote the answer if it is helpful. This works as a thanks here.

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 21:55











  • I can't because I have less than "15 reputation" :/

    – nch68
    Nov 20 '18 at 22:31













  • Oh, you are right :)

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 22:33



















  • thanks a lot. this does the trick

    – nch68
    Nov 20 '18 at 21:29











  • @nch68, please upvote the answer if it is helpful. This works as a thanks here.

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 21:55











  • I can't because I have less than "15 reputation" :/

    – nch68
    Nov 20 '18 at 22:31













  • Oh, you are right :)

    – Daniil Andreyevich Baunov
    Nov 20 '18 at 22:33

















thanks a lot. this does the trick

– nch68
Nov 20 '18 at 21:29





thanks a lot. this does the trick

– nch68
Nov 20 '18 at 21:29













@nch68, please upvote the answer if it is helpful. This works as a thanks here.

– Daniil Andreyevich Baunov
Nov 20 '18 at 21:55





@nch68, please upvote the answer if it is helpful. This works as a thanks here.

– Daniil Andreyevich Baunov
Nov 20 '18 at 21:55













I can't because I have less than "15 reputation" :/

– nch68
Nov 20 '18 at 22:31







I can't because I have less than "15 reputation" :/

– nch68
Nov 20 '18 at 22:31















Oh, you are right :)

– Daniil Andreyevich Baunov
Nov 20 '18 at 22:33





Oh, you are right :)

– Daniil Andreyevich Baunov
Nov 20 '18 at 22:33













1














Basically



let renumber = (obj, path) => {
obj.path = path
obj.children.forEach((c, n) => renumber(c, path.concat(n)))
}

renumber({children: yourData}, )


this creates a path property, which is an array of relative numbers. If you want to format it in a special way, then you can do



obj.path = format(path)


where format is like



let format = xs => xs.map(pad(4)).join(',')

let pad = w => x => (10 ** w + x).toString().slice(-w)





share|improve this answer


























  • thank you. I learned something today :)

    – nch68
    Nov 20 '18 at 21:29
















1














Basically



let renumber = (obj, path) => {
obj.path = path
obj.children.forEach((c, n) => renumber(c, path.concat(n)))
}

renumber({children: yourData}, )


this creates a path property, which is an array of relative numbers. If you want to format it in a special way, then you can do



obj.path = format(path)


where format is like



let format = xs => xs.map(pad(4)).join(',')

let pad = w => x => (10 ** w + x).toString().slice(-w)





share|improve this answer


























  • thank you. I learned something today :)

    – nch68
    Nov 20 '18 at 21:29














1












1








1







Basically



let renumber = (obj, path) => {
obj.path = path
obj.children.forEach((c, n) => renumber(c, path.concat(n)))
}

renumber({children: yourData}, )


this creates a path property, which is an array of relative numbers. If you want to format it in a special way, then you can do



obj.path = format(path)


where format is like



let format = xs => xs.map(pad(4)).join(',')

let pad = w => x => (10 ** w + x).toString().slice(-w)





share|improve this answer















Basically



let renumber = (obj, path) => {
obj.path = path
obj.children.forEach((c, n) => renumber(c, path.concat(n)))
}

renumber({children: yourData}, )


this creates a path property, which is an array of relative numbers. If you want to format it in a special way, then you can do



obj.path = format(path)


where format is like



let format = xs => xs.map(pad(4)).join(',')

let pad = w => x => (10 ** w + x).toString().slice(-w)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 15:53

























answered Nov 20 '18 at 12:28









georggeorg

148k35198297




148k35198297













  • thank you. I learned something today :)

    – nch68
    Nov 20 '18 at 21:29



















  • thank you. I learned something today :)

    – nch68
    Nov 20 '18 at 21:29

















thank you. I learned something today :)

– nch68
Nov 20 '18 at 21:29





thank you. I learned something today :)

– nch68
Nov 20 '18 at 21:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392644%2fsort-index-from-nested-json-with-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory