Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
Checking Nesting of objects

There can two sets of objects

First can be a straight forward where is no children

const noChildObj = [{
    "id": 1,
    "label": ["Description"],
    "lines": 1,
    "type": "string",
    "precision": 2,
    "width": 167.8
}, {
    "id": 2,
    "label": ["Information", "Ratio"],
    "lines": 2,
    "type": "number",
    "precision": 2,
    "width": 167.8
}, {
    "id": 3,
    "label": ["Tracking", "Error"],
    "lines": 2,
    "type": "number",
    "precision": 2,
    "width": 167.8
}]

So,in this case, there is no child , so property 'width' is at the topmost layer. Second is where nesting of objects occur

So each object in the array will have a child object


[{
    "id": "257",
    "label": [""],
    "lines": 1,
    "children": [{
        "id": "Description",
        "label": ["Dates"],
        "lines": 1,
        "type": "date",
        "precision": null,
        "width": 839
    }]
}, {
    "id": "258",
    "label": ["Cumulative Return"],
    "lines": 1,
    "children": [{
        "id": 12,
        "label": ["Russell 1000 Value - Price Return"],
        "lines": 1,
        "type": "number",
        "precision": 2,
        "width": 839
    }]
}]

Here each object has a child object where the property width exists

This nesting is not limited to just 1 level It can go upto 4 levels

So my use case requires if the innermost child has width undefined or not

I do realise that through recursion , we can traverse and find out.. But is there any function which can do it in less lines of code..may be in lodash

Please help

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Know only how to do with recursion:

const objArray = [{
    "id": 1,
    "label": ["Description"],
    "lines": 1,
    "type": "string",
    "precision": 2,
    "width": 167.8
}, {
    "id": "257",
    "label": [""],
    "lines": 1,
    "children": [{
        "id": "Description",
        "label": ["Dates"],
        "lines": 1,
        "type": "date",
        "precision": null,
        "width": 839
    }]
}]

const getWidthDeep = (el) => el.children && el.children[0]
  ? getWidthDeep(el.children[0])
  : el.width

const result = objArray.map(getWidthDeep)
console.log(result)

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!