Kenton Vizdos Kenton Vizdos - 2 months ago 10
Node.js Question

How to find position of an object in an array in JSON

Here is my data:

{
"bracketName": "Set Bracket Name",
"bracketId": "tTzbUZ",
"modID": "B11PTVjERm",
"creationDate": 1503352813796,
"teams": [{
"name": "team 1",
},
{
"name": "team 2",
},
{
"name": "team 2",
}
]
}


I am trying to get the position of the
team
. Here is my code:

var content = fs.readFileSync('brackets/' + data.bracket + '.json');
content = JSON.parse(content);
content.teams.indexOf({"name": "team 2"});


But it doesn't work. I'm not sure how to do this. Could somebody help me?

Answer Source
content.teams.findIndex(team => team.name == 'team 2')