Js multi-layer nesting, only know the last value, how to traverse to get all the parents

the data structure has multiple levels of nesting, as follows: the only value obtained so far is "521". How to traverse to get "1045-SCG" and "968"

:

[
  {
    "value": "1045-SCG",
    "label": "",
    "children": [
      {
        "value": "968",
        "label": "",
        "children": [{
          "value": "521",
          "label": "",
        }]
      }
    ]
  },
  {
    "value": "1044-SCG",
    "label": "111",
    "children": []
  },
  ,
  {
    "value": "1036-SCG",
    "label": "",
    "children": [
      {
        "value": "264",
        "label": "",
        "children": [
          {
            "value": "271",
            "label": "",
            "children": []
          }
        ]
      }]
  }
]
Jun.25,2022

simple objects have no record of which object attributes they are. You have to redesign the data structure


function l(data, val) {
  for(var i=0;i<data.length;iPP){
    if(data[i]&&data[i].value == val){
      return [];
    }
    if(data[i]&&data[i].children){
      var d = l(data[i].children,val);
      if(d)
        return d.concat(data[i].value);
    }
  }
}

l(data,'271');
Menu