0

I have a object array like bellow, that can goes to an unknown depth. I also have a string array that contain path (keys). path keys ["29be61bd-3eee-7e5d-abb2-8791c02ab842", "19be61bd-3eee-7e8d-abb2-8791c02ab841", "19be61bd-3eee-7e8d-abb2-8791c02ab841"]

[
  {
    "name": "background.png",
    "size": 3427,
    "__KEY__": "19be61bd-3eee-7e8d-abb2-8791c52av843",
    "content": "02610538-5847-4d50-b2cf-d4286bda2599",
    "isDirectory": false,
    "dateModified": "2022-07-06T14:33:57.634Z"
  },
  {
    "name": "background.png",
    "size": 3427,
    "__KEY__": "29be61bd-3eee-7e5d-abb2-8791c02ab842",
    "content": "7fbdd298-85b9-4839-8f75-58e0c7543ff1",
    "isDirectory": true,
    "dateModified": "2022-07-06T14:33:57.634Z",
    "items": [
        {
          "name": "47d10e6cc15beb354a4daadb4eb6a054 (1).gif",
          "size": 3427,
          "__KEY__": "19be61bd-3eee-7e8d-abb2-8791c02ab841",
          "isDirectory": true,
          "dateModified": "2022-07-05T19:58:04.707Z",
          "items": [
            {
              "name": "47d10e6cc15beb354a4daadb4eb6a054 (1).gif",
              "size": 3427,
              "__KEY__": "49be61bd-3eee-7e8d-abb2-8791c02ab843",
              "content": "adfkasbjfkjabsfjbas",
              "isDirectory": false,
              "dateModified": "2022-07-05T19:58:04.707Z"
            },
            {
                "name": "47d10e6cc15beb354a4daadb4eb6a055 (1).gif",
                "size": 3427,
                "__KEY__": "19be61bd-3eee-7e8d-abb2-8791c02ab841",
                "isDirectory": true,
                "dateModified": "2022-07-05T19:58:04.707Z",
                "items": [
                    {
                    "name": "47d10e6cc15beb354a4daadb4eb6a054 (1).gif",
                    "size": 3427,
                    "__KEY__": "19be61bd-3eee-7e8d-abb2-8791c02ab831",
                    "content": "dsvdsnakfna",
                    "isDirectory": false,
                    "dateModified": "2022-07-05T19:58:04.707Z"
                    }
                ]
              }
            
          ]
        }
    ]
  },
  {
    "name": "background.png",
    "size": 3427,
    "__KEY__": "background.png",
    "content": "4e977934-218f-4a18-a531-fc3d196bee7d",
    "isDirectory": false,
    "dateModified": "2022-07-06T14:33:57.634Z"
  },
  {
    "name": "background.png",
    "size": 3427,
    "__KEY__": "background.png",
    "isDirectory": true,
    "dateModified": "2022-07-06T14:33:57.634Z",
    "items": [
        {
        "name": "47d10e6cc15beb354a4daadb4eb6a054 (1).gif",
        "size": 3427,
        "__KEY__": "18be61bd-3eee-7e8d-abb2-8791c02ab831",
        "content": "dsvdsnakfna",
        "isDirectory": false,
        "dateModified": "2022-07-05T19:58:04.707Z"
        }
    ]
  }
]

I want to go to that path ( contains in the string array ) to the last folder and change recently added object in the "items" object array. How I develop this logic? Can anyone help me to find a way for this? I am using Type script to develop this and need a effective method. Any help is appreciated.

Samith Basnayaka
  • 137
  • 2
  • 15
  • Does this answer your question? [Accessing nested JavaScript objects and arrays by string path](https://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-and-arrays-by-string-path) – pilchard Jul 06 '22 at 21:32
  • also: [Access object child properties using a dot notation string](https://stackoverflow.com/questions/8051975/access-object-child-properties-using-a-dot-notation-string) – pilchard Jul 06 '22 at 21:33
  • why are there several elements with the same key in the same level? and which one to target? – Mister Jojo Jul 06 '22 at 22:10
  • 1
    This is what you're looking, many good solutions using recursive algorithms: https://stackoverflow.com/questions/58184779/update-nested-json-object-using-recursion-in-javascript – PsiKai Jul 07 '22 at 02:15
  • @MisterJojo please consider "__KEY__" as a unique identifier. – Samith Basnayaka Jul 07 '22 at 04:49
  • @PsiKai: That question and its answers explain how to make changes with a function applied at the leaf nodes. If I'm not mistaken, this is looking to change something at least one level higher. – Scott Sauyet Jul 07 '22 at 16:37
  • 1
    @ScottSauyet yeah, I agree it's not exactly the same, but the core concept is there. Happy to create a solution for OP, but didn't want to reinvent the wheel. – PsiKai Jul 07 '22 at 20:28

0 Answers0