0

I have this array of object

 [{chainTypeCode: "DIRECT", chainTypeId: "1"},
  {chainTypeCode: "MAGT", chainTypeId: "2"},
  {chainTypeCode: "MAGT_AGT", chainTypeId: "3"},
  {chainTypeCode: "MAGT_AGT_SAGT", chainTypeId: "4"}]

and I want to make a function getChainTypeCode(chainTypeId) in which if I pass a chainTypeId, it returns with a chainTypeCode

2 Answers2

0

You can make use of find()

getCode = (arr, id)=> arr.find(p=>p.chainTypeId == id).chainTypeCode
gorak
  • 5,233
  • 1
  • 7
  • 19
0

You can use find

function getChainTypeCode(chainTypeId, chainTypeList) {
    return chainTypeList.find(chainType => chainType.chainTypeId == chainTypeId).chainTypeCode;
}

var obj = [{chainTypeCode: "DIRECT", chainTypeId: "1"},
  {chainTypeCode: "MAGT", chainTypeId: "2"},
  {chainTypeCode: "MAGT_AGT", chainTypeId: "3"},
  {chainTypeCode: "MAGT_AGT_SAGT", chainTypeId: "4"}];

var result = getChainTypeCode(1, obj);
console.log(result);
JoinCompany
  • 504
  • 2
  • 11