-1

I want a function that takes the name of the row in the JSON file and returns a list of elements from that row based on that argument... if "DRIVER" is called then I should get all the drivers in that row, if "COMPANY" is called, then all the companies...I've done several attempts to get it, like concatinating +".argument" but it does not seem to work. Any help will be greatly appreciated..

const data = require('./drivers.json'); // THIS IS MY FILE 

function getInfo(argument){
    console.log(argument);
    for (let i = 0; i < data.length; i+=1) {
        console.log(data1[i].argument);       //UNDEFINED 
    }

}

getInfo('DRIVER');

2 Answers2

0

If you have an object obj = {hello: 'World'), and a variable var = 'hello', access it like this obj[var].

Your current implementation will retrieve obj['argument'], which is not defined.

Cody E
  • 179
  • 11
-1

There is a mistake in your code. You are using data as input to the for loop, but in the loop you use data1. This should fix it:

for (let i = 0; i < data.length; i+=1) {
    console.log(data[i].argument);       
}

There is no way your code will ever work with your typo in it.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Klaassiek
  • 2,795
  • 1
  • 23
  • 41