1

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.

1
  • Is the length of the Id: 123 part 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? Commented Oct 28, 2019 at 15:58

3 Answers 3

2

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);

Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.