-3

I have JSON content as follows

jsonList = {
    "0": {
        "Name": "Virat Kohli",
        "Runs": "6283",
        "Matches": "207",
        "Average": "37.39",
        "Strike Rate": "129.94",
        "Half Centuries": "42",
        "Centuries": "5",
        "Team": "Royal Challengers Bangalore"
    },
    "1": {
        "Name": "Shikhar Dhawan",
        "Runs": "5784",
        "Matches": "192",
        "Average": "34.84",
        "Strike Rate": "126.64",
        "Half Centuries": "44",
        "Centuries": "2",
        "Team": "Delhi Capitals"
    },
    "2": {
        "Name": "Rohit Sharma",
        "Runs": "5611",
        "Matches": "213",
        "Average": "31.17",
        "Strike Rate": "130.39",
        "Half Centuries": "40",
        "Centuries": "1",
        "Team": "Mumbai Indians"
    },
    "3": {
        "Name": "Suresh Raina",
        "Runs": "5528",
        "Matches": "205",
        "Average": "32.51",
        "Strike Rate": "136.76",
        "Half Centuries": "39",
        "Centuries": "1",
        "Team": "Chennai Super Kings"
    }
}

I get the value of "Virat Kohli" as

name = jsonList[0].Name

I am working to get all the Names in one array and Runs in one array like

names = ['Virat Kohli','Shikhar Dhawan','Rohit Sharma','Suresh Raina']
runs = [6283,5784,5611,5528]

I know I can do this using for loop. But is there any other way that is more efficient than for loop?

Rahul
  • 13
  • 3

1 Answers1

1

First, transform that Obj into an array:

const arr = Object.values(jsonList)

Then, get the information you need by using .map function:

const names = arr.map(el=> el.Name)
const runs = arr.map(el=> el.Runs)
Elson Ramos
  • 771
  • 4
  • 13