Read json file content with require vs fs.readFile

前端 未结 7 2000
说谎
说谎 2020-11-30 03:46

Suppose that for every response from an API, i need to map the value from the response to an existing json file in my web application and display the value from the json. Wh

7条回答
  •  既然无缘
    2020-11-30 04:23

    {
      "country": [    
        "INDIA",
        "USA"
      ],
      "codes": [   
        "IN",
        "US"
      ]
    }
    
    // countryInfo.json
    
    const { country, code } = require('./countryInfo.json');
    
    console.log(country[0]); // "INDIA"
    console.log(code[0]); // "IN"
    

提交回复
热议问题