JavaScript:是否可以根据对象的键访问对象数组中的对象?

时间:2019-06-22 11:48:33

标签: javascript

我有以下对象数组:

 EachRiskMappedToMaxImpact = [{
        riskid: 'R01',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R02',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R03',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R04',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R05',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R06',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R07',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R08',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R09',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R10',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R11',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R12',
        maxImpact: 'E. Not significant'
    },{
        riskid: 'R13',
        maxImpact: 'E. Not significant'
    },{
        riskid: 'R14',
        maxImpact: 'E. Not significant'
    },{
        riskid: 'R15',
        maxImpact: 'E. Not significant'
    },{
        riskid: 'R16',
        maxImpact: 'E. Not significant'
    },{
        riskid: 'R17',
        maxImpact: 'E. Not significant'
    }, ,{
        riskid: 'R18',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R19',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R20',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R21',
        maxImpact: 'E. Not significant'
    }
];

我想知道是否可以通过使用键来访问数组内部的对象。
例如:
我要分配

'B. Very serious'

第一个对象的maxImpact属性:

{
        riskid: 'R01',
        maxImpact: 'E. Not significant'
    }

我不想使用

EachRiskMappedToMaxImpact[0].maxImpact='B. Very serious'

我想根据其主要的Riskid访问第一个对象。
这可能吗?

2 个答案:

答案 0 :(得分:1)

正如您所说的,您要根据第一个对象的主要Riskid访问它。您可以使用.map()遍历每个元素并更改数组。

var EachRiskMappedToMaxImpact = [{
        riskid: 'R01',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R02',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R03',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R04',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R05',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R06',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R07',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R08',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R09',
        maxImpact: 'E. Not significant'
    }, {
        riskid: 'R21',
        maxImpact: 'E. Not significant'
    }
];

var b=EachRiskMappedToMaxImpact.map(function(a){
  if(a.riskid=='R01'){
    a.maxImpact='B. Very serious'
  }

  return a;
});
console.log(b);

Demo

答案 1 :(得分:0)

EachRiskMappedToMaxImpact.forEach( element => {
 if(element.riskid == "R01"){
    element.maxImpact = "B. Very serious"
 }
})


//where in if condition "R01" will be your key for which you want to search
//and after that set the maxImpact to your desired value