-1

I have array of object like this :

enter image description here

I need to select only the array which has coreSystemID = 'T24' so I tried credsArr.find() it shows my .find is not a function.

below is my code:

var creds = credsArr.find(x => x.coreSystemID === 'T24')

And I should use the index value like this - credsArr[3053], because the index may vary so I need to select the array which has coreSystemID = 'T24'

Moon
  • 4,014
  • 3
  • 30
  • 66
Mar1009
  • 721
  • 1
  • 11
  • 27
  • 6
    `credsArr` is an object, not an array, the same for its values, 3052 and 3053... – Calvin Nunes Dec 02 '19 at 11:37
  • 1
    If you don't need the key (3053): `Object.values(credsArr).find(cred => cred.coreSystemID === "T24")` –  Dec 02 '19 at 11:41

2 Answers2

2
let keys = Oject.keys(credsArr).filter(key => credsArr[key].coreSystemID==='T24')

keys will have all the keys of credsArr having credsArr[key].coreSystemID as 'T24'

let systemT24 = credsArr[keys[0]] will give you the object you are looking for given that there is only one object with coreSystemID as 'T24'.

If you have multiple objects that might have coreSystemID as 'T24' you can map it to a new array like this:

let systemT24 = keys.map(key => credsArr[key])
kooskoos
  • 4,622
  • 1
  • 12
  • 29
1

You can use Object.values(credsArr):

 var creds = Object.values(credsArr).find(x => x.coreSystemID === 'T24');

or if you need key of the object:

var creds = Object.entries(credsArr).find(x => x[1].coreSystemID === 'T24');
let key = creds[0];
let value = creds[1];
Oleg
  • 3,580
  • 1
  • 7
  • 12