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?

4
  • stackoverflow.com/a/24012023/6446770 this might help Commented Sep 26, 2018 at 9:16
  • what have you tried so far Commented Sep 26, 2018 at 9:16
  • Iterate through your array and then access the said key using either dot notation or bracket notation. Commented Sep 26, 2018 at 9:28
  • Could you show an example please? Commented Sep 26, 2018 at 9:31

3 Answers 3

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"])
})

Sign up to request clarification or add additional context in comments.

2 Comments

I want to do this in java. Not in javascript
@KasunChathuranga then post the appropriate tags for your question. As of this moment you have [javascript] tag and no [java] tag.
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.

Comments

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"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.