简体   繁体   English

如何在JSON中的数组中查找对象的位置

[英]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 . 我正在努力获得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? 有人可以帮我吗?

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

You can use findIndex . 您可以使用findIndex For example: 例如:

content.teams.findIndex(x => x.name=="team 2")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM