Jim Dover
Jim Dover

Reputation: 623

Substring in array of objects

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.

Upvotes: 1

Views: 1538

Answers (3)

VLAZ
VLAZ

Reputation: 29047

You can use simple array manipulation if you just extract the portion of text from after Id: (four characters, so up to index 3 in the string) and the first comma that would appear after the number:

const result = [ { ID: 1, Reference: 'Id: 123, Name: "first' }, { ID: 2, Reference: 'Name: "second' }, { ID: 3, Reference: 'Id: 133, Name: "third' } ]; 

function extractId(reference) {
  let from = 3;
  let to = reference.indexOf(",");
  
  return reference.slice(from, to);
}

let ids = result
  .filter(i => i.Reference.includes('Id:'))
  .map(i => i.Reference)
  .map(extractId)
  .map(Number);
  

console.log(ids);

Alternatively, you can use a regular expression to capture and extract the ID

const result = [ { ID: 1, Reference: 'Id: 123, Name: "first' }, { ID: 2, Reference: 'Name: "second' }, { ID: 3, Reference: 'Id: 133, Name: "third' } ]; 

function extractId(reference) {
  let regex = /Id: (\d+)/;
  
  return reference.match(regex)[1];
}

let ids = result
  .filter(i => i.Reference.includes('Id:'))
  .map(i => i.Reference)
  .map(extractId)
  .map(Number);
  

console.log(ids);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386654

You could map the part with a regular expression for the digits and return a number.

const
    result = [{ ID: 1, Reference: 'Id: 123, Name: "first' }, { ID: 2, Reference: 'Name: "second' }, { ID: 3, Reference: 'Id: 133, Name: "third' }],
    filter = result
        .filter(i => i.Reference.includes('Id:'))
        .map(i => i.Reference)
        .map(s => +s.match(/Id:\s*(\d+)/)[1])

console.log(filter);

Upvotes: 2

kockburn
kockburn

Reputation: 17616

Using regex you can strip your number from your string

const Reference = 'Id: 133, Name: "third'

console.log(
  (/Id:\s(\d+),/g).exec(Reference)[1]
);

Final solution:

const result = [
  {
    ID: 1,
    Reference: 'Id: 123, Name: "first'
  },
  {
    ID: 2,
    Reference: 'Name: "second'
  },
  {
    ID: 3,
    Reference: 'Id: 133, Name: "third'
  }
];

const res = result
  .map(({Reference})=>+((/Id:\s(\d+),/g).exec(Reference)||[])[1])
  .filter(item=>!!item)

console.log(res);

Upvotes: 2

Related Questions