.ajax({
cache: false,
url: '/Dashboard/ListofNames',
data: {
charSearch: txtboxvalue
},
success: function (res){
var obj = JSON.parse(res); //this object contains 1 index which is 0.
var parseObj = JSON.parse(obj[0].JsonResult) // This line is working i successfully gets the data from index 0
var parseObj2 = JSON.parse(obj[1].JsonResult) //This is not working because index 1 is not existing, what i want is instead of getting an undefined exception, i want to set the index 1 into an empty string or whatever solution that i wont get any undefined exception.
}
});
Any suggestion would really help me thanks.
Considering your JSON.parse of object given the response as this object.
let res = [
{ name: "john", id: 1, JsonResult: "json-stringified-object-or-string" }
]
JSON.parse(res[0] ? res[0].JsonResult : '""');
JSON.parse(res[1] ? res[1].JsonResult : '""');
Found out a solution. I checked all of the undefined obj and set a new value for each. I created a loop to check every undefined obj after that i created a new object with Empty string and push it inside my jsonresult. Not a good practice, but it does the job.