0

I am new to Javascript and I struggle with arrays and objects. I am trying to find a value/attribute of a JSON file using the method find. This is the data on the JSON file named "data":

{
    "one": {
        "color": "green",
        "amount": 1
    },
    "two": {
        "color": "green",
        "amount": 1
    }
}

Then I use this code to execute the method find to get the information that in this case is the number "one".

var data = fs.readFileSync("data.json");
var pData = JSON.parse(data);

var isOne = "one";
var match = pData.find(nmb => {
    if (isOne === nmb) {
        return true;
    } else {
        console.log("error");
    }
});

As the code start to run the, I get an error called "pData.find() is not a function at Object.". I tried to stringify the data and still can't access the attribute "one".

Russopotomus
  • 595
  • 3
  • 8
papisilv
  • 41
  • 1
  • 7

0 Answers0