如何在 JSON 的数组中查找对象的位置
How to find position of an object in an array in JSON
这是我的数据:
{
"bracketName": "Set Bracket Name",
"bracketId": "tTzbUZ",
"modID": "B11PTVjERm",
"creationDate": 1503352813796,
"teams": [{
"name": "team 1",
},
{
"name": "team 2",
},
{
"name": "team 2",
}
]
}
我正在尝试获取 team
的位置。这是我的代码:
var content = fs.readFileSync('brackets/' + data.bracket + '.json');
content = JSON.parse(content);
content.teams.indexOf({"name": "team 2"});
但是没用。我不知道该怎么做。有人可以帮助我吗?
content.teams.findIndex(team => team.name == 'team 2')
您可以使用 findIndex
。例如:
content.teams.findIndex(x => x.name=="team 2")
这是我的数据:
{
"bracketName": "Set Bracket Name",
"bracketId": "tTzbUZ",
"modID": "B11PTVjERm",
"creationDate": 1503352813796,
"teams": [{
"name": "team 1",
},
{
"name": "team 2",
},
{
"name": "team 2",
}
]
}
我正在尝试获取 team
的位置。这是我的代码:
var content = fs.readFileSync('brackets/' + data.bracket + '.json');
content = JSON.parse(content);
content.teams.indexOf({"name": "team 2"});
但是没用。我不知道该怎么做。有人可以帮助我吗?
content.teams.findIndex(team => team.name == 'team 2')
您可以使用 findIndex
。例如:
content.teams.findIndex(x => x.name=="team 2")