Home > Software engineering >  Update nested array with conditions in javascript
Update nested array with conditions in javascript

Time:10-27

I want to create nested array in JavaScript. I will be getting object like this as input: [{size: 12, "text": "aa"}, {size: 11, "text": "ab"}, {size: 10, "text": "ac"}, {size: 12, "text": "ad"}...]

I want to compare size and create array like structure given below. I wanted to use references to create the same, but JavaScript doesn't have references. The o/p array will be like this:

[{
        "size": 12,
        "text": "a",
        "childrens": [{
                "size": 11,
                "text": "a",
                "childrens": [{
                        "size": 10,
                        "text": "a",
                        "childrens": [{
                            "size": 9,
                            "text": "a",
                            "childrens": []
                        }]
                    },
                    {
                        "size": 10,
                        "text": "a",
                        "childrens": []
                    }
                ]
            },
            {
                "size": 11,
                "text": "a",
                "childrens": [{
                        "size": 10,
                        "text": "a",
                        "childrens": [{
                            "size": 9,
                            "text": "a",
                            "childrens": []
                        }]
                    },
                    {
                        "size": 10,
                        "text": "a",
                        "childrens": []
                    }
                ]
            }
        ]
    },
    {
        "size": 12,
        "text": "a",
        "childrens": [{
                "size": 11,
                "text": "a",
                "childrens": [{
                        "size": 10,
                        "text": "a",
                        "childrens": [{
                            "size": 9,
                            "text": "a",
                            "childrens": []
                        }]
                    },
                    {
                        "size": 10,
                        "text": "a",
                        "childrens": []
                    }
                ]
            },
            {
                "size": 11,
                "text": "a",
                "childrens": [{
                        "size": 10,
                        "text": "a",
                        "childrens": [{
                            "size": 9,
                            "text": "a",
                            "childrens": []
                        }]
                    },
                    {
                        "size": 10,
                        "text": "a",
                        "childrens": []
                    }
                ]
            }
        ]
    }
]

I tried this:

let start = [];
inp.forEach(key => {
   let found = false;
   while(!found) {
      if(start.length === 0 || start[start.length - 1].size === key.size || key.size > start[start.length - 1].size ) {
         start.push({...key, childrens: []})
    found = true;
      }
      else if(start[start.length - 1].size > key.size) {
         start = start[start.length - 1].childrens;
      }
  }
})

But in this I know I am reassigning start, which seems to be wrong. So How can I create an array structure given above? Thanks in advance.

CodePudding user response:

let inputArr = [{size: 12, "text": "aa"}, {size: 11, "text": "ab"}, {size: 10, "text": "ac"}, {size: 12, "text": "ad"}]
let outputArr = [];

let findPlace = (output, elem) => {
  if (output.length === 0) {
    output.push(elem)
  }
  else {
    let latestOuputElem = output[output.length - 1]
    if (elem.size >= latestOuputElem.size) {
      output.push(elem)
    } else {
      if (!latestOuputElem.childrens) {
        latestOuputElem.childrens = []
      }
      findPlace(latestOuputElem.childrens, elem)
    }
  }
}

inputArr.forEach(elem => findPlace(outputArr, elem))

// result
// outputArr = [{"size":12,"text":"aa","childrens":[{"size":11,"text":"ab","childrens":[{"size":10,"text":"ac"}]}]},{"size":12,"text":"ad"}]
  • Related