How do I access this JSON values?

·

I am working on an Income tax calculator in JS based on Indian tax slabs.

I am new to JSON and JS. Can anyone tell me how can I access values from this and compare it with user input and do the calculation?

Conditions:

#1. If User chose 2019_20 as year, it should fetch value from 2019_20.

#2. After that user choose the age and so, if 2019_20 is selected, and age 60 or below is selected, we have to fetch value from "60" in "2019_20".

var taxSlab = {
    "2019_20" : {
        "60": [
        { "taxableAnnualIncome_from": 0, "taxableAnnualIncome_to": 250000, "taxPercentage": 0 },
        { "taxableAnnualIncome_from": 250000, "taxableAnnualIncome_to": 500000, "taxPercentage": 5 },
        { "taxableAnnualIncome_from": 500000, "taxableAnnualIncome_to": 100000, "taxPercentage": 10 },
        { "taxableAnnualIncome_from": 100000, "taxableAnnualIncome_to": 1000000, "taxPercentage": 20 },
                ],

        "80": [
        { "taxableAnnualIncome_from": 0, "taxableAnnualIncome_to": 250000, "taxPercentage": 0 },
        { "taxableAnnualIncome_from": 250000, "taxableAnnualIncome_to": 500000, "taxPercentage": 5 },
        { "taxableAnnualIncome_from": 500000, "taxableAnnualIncome_to": 100000, "taxPercentage": 10 },
        { "taxableAnnualIncome_from": 100000, "taxableAnnualIncome_to": 1000000, "taxPercentage": 20 },
                ] 
            },

    "2018_19" : {
        "60": [
        { "taxableAnnualIncome_from": 0, "taxableAnnualIncome_to": 250000, "taxPercentage": 0 },
        { "taxableAnnualIncome_from": 250000, "taxableAnnualIncome_to": 500000, "taxPercentage": 5 },
        { "taxableAnnualIncome_from": 500000, "taxableAnnualIncome_to": 100000, "taxPercentage": 10 },
        { "taxableAnnualIncome_from": 100000, "taxableAnnualIncome_to": 1000000, "taxPercentage": 20 },
                        ],

        "80": [
        { "taxableAnnualIncome_from": 0, "taxableAnnualIncome_to": 250000, "taxPercentage": 0 },
        { "taxableAnnualIncome_from": 250000, "taxableAnnualIncome_to": 500000, "taxPercentage": 5 },
        { "taxableAnnualIncome_from": 500000, "taxableAnnualIncome_to": 100000, "taxPercentage": 10 },
        { "taxableAnnualIncome_from": 100000, "taxableAnnualIncome_to": 1000000, "taxPercentage": 20 },
                        ] 
            },
}

Comments (1)

King Bee's photo

What you're looking for is object accessors. Check it out in the official docs.

//Your code will probably look something like this in the end:
...
function selectYear(year){
   return taxSlab[year];
}

function selectAgeOrBelow(obj, age){
   //If an exact value exists. If not, you have to go through the whole 
   //object using Object.entries or hasOwnProperty and return the smallest
  //value. That can then be returned instead.
   return obj[age]; //an array
}
//And you can use them like:
let userObject = selectAgeOrBelow(selectYear("2019_20"), "60");  //an array

//Of course, you can use all of that in one line if you prefer (no need for multiple functions, but it helps)