Tengo contenido JSON de la siguiente manera
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" } }Obtengo el valor de "Virat Kohli" como
name = jsonList[0].NameEstoy trabajando para obtener todos los nombres en una matriz y se ejecuta en una matriz como
names = ['Virat Kohli','Shikhar Dhawan','Rohit Sharma','Suresh Raina'] runs = [6283,5784,5611,5528]Sé que puedo hacer esto usando for loop. Pero, ¿hay alguna otra forma que sea más eficiente que for loop?
Primero, transforma ese Obj en una matriz:
const arr = Object.values(jsonList)Luego, obtenga la información que necesita usando la función .map:
const names = arr.map(el=> el.Name) const runs = arr.map(el=> el.Runs)