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

128
Views
check if item exists in json file then get it if exists. And if not complete executing the code

I get a json file from outer link by ajax call. i want to check element is exists in this json file or not and get it if exists. because it changes every time and items increases or decreases. If I get item and this item not existing, I get error and the code not complete. i want to check this item json.children[1].children and json.children[2].children ,..... if exists.

{
    "num": 1,
    "name": "aa",
    "properites": [
        {
            "name": "prop1",
            "value": "value1"
        }
    ],
    "children": [
       
        {
            "num2": 1,
            "name2": "name2",
            "properites": [
                {
                    "name": "name",
                    "value": "value"
                }
            ],
            "children": []
        },
       
        {
            "name": 1,
            "num": "1",
            "attributes": [
                {
                    "name": "name",
                    "value": "value"
                }
            ],
            "children": []
        .......................... big json file

I have tried to get these Items and set in local storage to use in table by this:

localStorage.setItem("row1-item1", json.children[1].children);
    localStorage.setItem("row1-item2", json.children[2].children);
    localStorage.setItem("row1-item3", json.children[3].children);
    localStorage.setItem("row1-item4", json.children[4].children);
    localStorage.setItem("row2-item1", json.name[1].children);
    localStorage.setItem("row2-item2", json.name[2].children);
    localStorage.setItem("row2-item3", json.name[3].children);
    localStorage.setItem("row2-item4", json.name[4].children);

the problem here is if json.children[4].children not existing the code not complete the next line and jump out of ajax call even if json.name[1].children is exists. I tried try/catch but not solved it

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try to use map

json.children.map((children,index)=>{
    localStorage.setItem('row${index}-item${index}`, children);
}

Or you can use forEach or for loop instead of map.

about 4 years ago · Juan Pablo Isaza 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!