Using Node.js 10.*
I have the following data structure being returned to me:
const result = [
{
ID: 1,
Reference: 'Id: 123, Name: "first'
},
{
ID: 2,
Reference: 'Name: "second'
},
{
ID: 3,
Reference: 'Id: 133, Name: "third'
}
];
I want to capture the Id of each Reference if it exists, and push to a new array, which would give me the following:
// [123,133]
I can use Filter and Map to filter out which does not contain 'Id' in Reference by the following:
let filter = result.filter(i => i.Reference.includes('Id:')).map(i => i.Reference)
Which gives me:
// ['Id': 123, Name: 'first, 'Id': 133, Name: 'third']
So from the array above, I was to just strip out the Id to get:
// [123,133]
Using sub strings doesn't seem to work for me.
Id: 123part of the string known? If so, you can just extract that part. If not, then is the structure always the same?Id: <digits>followed by a comma?