Home > Net >  Sort array of objects based on parent ID without for loops or forEach
Sort array of objects based on parent ID without for loops or forEach

Time:01-16

Given the following object array I want all longest possible folder directories based on parent ids, seperated by commas

All outputs

  • base,0
  • base,12 (12 has a parent id of base)
  • base,1
  • base,1,2,9 (9 has a parent id of 2, 2 has a parent id of 1)
  • base,1,3
  • base,1,4,5,6,7,8 (all linked)

I have tried the solutions from this stackoverflow answer

However, One: Using a for loop or a foreach function is too slow for my website Two: It does not output the same result as I want.

I currently have this

<script>
  const data = [{
        "id": "12",
        "parent_id": "base",
        "name": "",
        "contents": ["Knowledge Base.pdf", "Knowledge.pdf"]
      }, {
        "id": "0",
        "parent_id": "base",
        "name": "Test Folder 1",
        "contents": ["81321-ksdjncewks.docx", ".pdf"]
      }, {
        "id": "1",
        "parent_id": "base",
        "name": "Test Folder 2",
        "contents": ["jmjmtj.docx", "thyfjd.pdf", "hdfjfj.xlsx", "dyjyk.pptx", "adad.jpg", ",k,ya.png"]
      }, {
        "id": "2",
        "parent_id": "1",
        "name": "Test Folder 3",
        "contents": ["dg.docx", "tj,j,h.pdf", "yjhas.xlsx", "thjyrsku.pptx",
          "AWGWR.jpg", "greht.png"
        ]
      }, {
        "id": "3",
        "parent_id": "1",
        "name": "Test Folder 4",
        "contents": ["mmmm.docx", "bbbb.pdf", "zzzz.xlsx", "xxxx.pptx", "ccc.jpg", "vvv.png"]
      }, {
        "id": "4",
        "parent_id": "1",
        "name": "Test Folder 5",
        "contents": ["qqqqqq.docx",
          "wwww.pdf", "eeee.xlsx", "rrrr.pptx", "ttttt.jpg", "yyyy.png"
        ]
      }, {
        "id": "5",
        "parent_id": "4",
        "name": "Test Folder 6",
        "contents": ["nooo.docx", "hi.pdf", "wassup.xlsx", "nice.pptx"]
      }, {
        "id": "6",
        "parent_id": "5",
        "name": "Test Folder 7",
        "contents": ["nydnooo.docx", "hhdjhi.pdf", "wndassup.xlsx", "nidfyce.pptx"]
      }, {
        "id": "7",
        "parent_id": "6",
        "name": "Test Folder 8",
        "contents": ["nohmgjmoo.docx", "hk,kvi.pdf", "wassu,jv,f.xlsx", "nicchmchvnve.pptx"]
      }, {
        "id": "8",
        "parent_id": "7",
        "name": "Test
        Folder 9 ", "
        contents ": ["
        nmhmxooo.docx ", "
        hhdjdhi.pdf ", "
        wasmjmvsup.xlsx ", "
        niddnhgdgce.pptx "] }, { "
        id ": "
        9 ", "
        parent_id ": "
        2 ", "
        name ": "
        Test Folder 10 ", "
        contents ": ["
        nqfefrsgooo.docx ", "
        advdhi.pdf ", "
        wafasdfjyjsup.xlsx ", "
        nifgghjdce.pptx "] } ]

    let results = data.reduce((results, data) => {
(results[data.parent_id] = results[data.parent_id] || []).push(data);
return results;
}, {})
console.log(results)
</script>

CodePudding user response:

You could build an object and get all pathes.

const
    getPathes = (object, key) => object[key]
        ?.flatMap(id => getPathes(object, id).map(a => [key, ...a]))
        ?? [[key]],
    data = [{ id: "12", parent_id: "base", name: "", contents: ["Knowledge Base.pdf", "Knowledge.pdf"] }, { id: "0", parent_id: "base", name: "Test Folder 1", contents: ["81321-ksdjncewks.docx", ".pdf"] }, { id: "1", parent_id: "base", name: "Test Folder 2", contents: ["jmjmtj.docx", "thyfjd.pdf", "hdfjfj.xlsx", "dyjyk.pptx", "adad.jpg", ",k,ya.png"] }, { id: "2", parent_id: "1", name: "Test Folder 3", contents: ["dg.docx", "tj,j,h.pdf", "yjhas.xlsx", "thjyrsku.pptx", "AWGWR.jpg", "greht.png"] }, { id: "3", parent_id: "1", name: "Test Folder 4", contents: ["mmmm.docx", "bbbb.pdf", "zzzz.xlsx", "xxxx.pptx", "ccc.jpg", "vvv.png"] }, { id: "4", parent_id: "1", name: "Test Folder 5", contents: ["qqqqqq.docx", "wwww.pdf", "eeee.xlsx", "rrrr.pptx", "ttttt.jpg", "yyyy.png"] }, { id: "5", parent_id: "4", name: "Test Folder 6", contents: ["nooo.docx", "hi.pdf", "wassup.xlsx", "nice.pptx"] }, { id: "6", parent_id: "5", name: "Test Folder 7", contents: ["nydnooo.docx", "hhdjhi.pdf", "wndassup.xlsx", "nidfyce.pptx"] }, { id: "7", parent_id: "6", name: "Test Folder 8", contents: ["nohmgjmoo.docx", "hk,kvi.pdf", "wassu,jv,f.xlsx", "nicchmchvnve.pptx"] }, { id: "8", parent_id: "7", name: "Test Folder 9", contents: ["nmhmxooo.docx", "hhdjdhi.pdf", "wasmjmvsup.xlsx", "niddnhgdgce.pptx"] }, { id: "9", parent_id: "2", name: "Test Folder 10", contents: ["nqfefrsgooo.docx", "advdhi.pdf", "wafasdfjyjsup.xlsx", "nifgghjdce.pptx"] }],
    references = data.reduce((r, { id, parent_id }) => {
        (r[parent_id] ??= []).push(id);
        return r;
    }, {}),
    result = getPathes(references, 'base').map(a => a.join());

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

  • Related