在Json值中查找名称ID时遇到问题

时间:2019-01-15 08:19:11

标签: angular ionic-framework angular5 angular6 angular-services

我正在Angular项目上工作,我在存储名称ID时遇到问题 变量

示例:

[
{ttypeName: Cricket , ttypeUid: 1}
 {ttypeName: Hockey, ttypeUid: 2} , 
{ttypeName: nba, ttypeUid: 3}  , 
{ttypeName: football, ttypeUid:4}  , 
{ttypeName: kabadi, ttypeUid: 5}  ]

我们有名称,现在我们要匹配 MatchId 并将其存储在变量中并使用

注意:JASON值不是静态的,长度和顺序不是固定的,这些值来自服务

我在下面发布我的代码->

解释我正在包含 ttypeName 的函数中传递 sportstype ,我想获取的是 ttypeUid

为此,我编写代码:

this.responsesportstypedata.find(m => m.ttypeName == sportstype ).ttypeUid;

输出: 但这返回了错误的id, 例如,它返回ttypeUid:3表示ttypeName:板球;

getmatchDetails(sportstype){

           this.rest.getSportstype().then(
                 result => {
                  this.responsesportstypedata = result;


                  this.sportsUid =   this.responsesportstypedata.find(m => m.ttypeName == sportstype ).ttypeUid;


                },
                   err => {
                  this.router.navigate(['ErrorPage']);
                     }
                  );

2 个答案:

答案 0 :(得分:0)

这里需要的是array.find()

let matchElement = results.find(t=>matchId == yourId);

答案 1 :(得分:0)

应该是我所理解的问题

const squaredIntegers = arr.filter( (num) => num > 0 && num % 1 === 0 ).map( (num) => num * num );