简体   繁体   中英

javascript json array using javascript for getting key/value pair

Before I start to write my question saying that I had tried with available solutions from stackoverflow but not able to fit my requirement. Please tell me any.

Please check the image in which I have an JSON array format. I want javascript loop in which I want "value" parameter from each options.

Had tried with below code : If I console the statelist then it is showing the result as in below format.

options0: {name: "Andaman and Nicobar Islands", value: "1", contact: false}
options1: {name: "Andhra Pradesh", value: "2", contact: false}
options2: {name: "Arunachal Pradesh", value: "3", contact: false}
options3: {name: "Assam", value: "4", contact: false}


for(var i = 1; i <= Object.keys(statelists).length; i++) {
        var obj = statelists.options[i].value;
        console.log(obj);
    }

ANSWER EDITED TO REFLECT COMMENT - As noted in the comments - the content of the question is not JSON - but is an object that contains other objects. You can get an array of the values of each object using Object.values(statelist) and then simply iterate over that array with a .forEach loop and console.log() the value of each object.

 var statelist = { options0: {name: "Andaman and Nicobar Islands", value: "1", contact: false}, options1: {name: "Andhra Pradesh", value: "2", contact: false}, options2: {name: "Arunachal Pradesh", value: "3", contact: false}, options3: {name: "Assam", value: "4", contact: false} }; var options = Object.values(statelist); // gives an array of the values of each object options.forEach(function(option) { console.log(option.value); // gives the value of each iteration eg: 1, 2, 3, 4 })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM