0

I have a JSON string of multiple arrays obtained from the front end of my application.

String editList = [{"gradingId":"9","lowerLimit":"34","upperLimit":"55"},{"gradingId":"11","lowerLimit":"23","upperLimit":"45"},{"gradingId":"37","lowerLimit":"20","upperLimit":"35"}]

i obtain individual elements as,

gradingId= 9
lowerLimit=34 
UpperLimit=55

and so on.

is there a way that I can access the gradingId, LowerLimit and upperLimit by there names?

3 Answers3

1

This will help you loop through the data

let editList = [{"gradingId":"9","lowerLimit":"34","upperLimit":"55"},{"gradingId":"11","lowerLimit":"23","upperLimit":"45"},{"gradingId":"37","lowerLimit":"20","upperLimit":"35"}]

editList.map(listItem => {
  Object.keys(listItem).map(objItem => {
    console.log(objItem +'=' + listItem[objItem])
  })
})

Or if the keys are predefined. You can avoid the second loop and make use of the below code.

let editList = [{"gradingId":"9","lowerLimit":"34","upperLimit":"55"},{"gradingId":"11","lowerLimit":"23","upperLimit":"45"},{"gradingId":"37","lowerLimit":"20","upperLimit":"35"}]

editList.map(listItem => {
  console.log("gradingId" +'=' + listItem["gradingId"])
  console.log("lowerLimit" +'=' + listItem["lowerLimit"])
  console.log("upperLimit" +'=' + listItem["upperLimit"])
})
Abin Thaha
  • 4,493
  • 3
  • 13
  • 41
0
let editList = [
{"gradingId":"9","lowerLimit":"34","upperLimit":"55"},
{"gradingId":"11","lowerLimit":"23","upperLimit":"45"},
{"gradingId":"37","lowerLimit":"20","upperLimit":"35"}]

editList.forEach((list,index) => {
   console.log(editList[index]);
)

you could use forEach function for array in javascript.

0
  1. Iterate through editList as an array with a for loop, Array method, etc.
  2. Assign a variable to an Object property using bracket notation.
editList[i][key]

var editList = [{"gradingId": "9","lowerLimit": "34","upperLimit": "55"}, {"gradingId": "11","lowerLimit": "23","upperLimit": "45"}, {"gradingId": "37","lowerLimit": "20","upperLimit": "35"}];

function propVal(arr, key) {
  var res = [];
  for (let i = 0; i < arr.length; i++) {
    res.push(arr[i][key]);
  }
  return `${key}: ${res}`;
}

console.log(propVal(editList, "gradingId"));
console.log(propVal(editList, "lowerLimit"));
console.log(propVal(editList, "upperLimit"));
zer00ne
  • 41,936
  • 6
  • 41
  • 68