简体   繁体   English

对象数组中的 Substring

[英]Substring in array of objects

Using Node.js 10.*使用 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:我想捕获每个引用的 Id(如果存在),然后推送到一个新数组,这将为我提供以下信息:

// [123,133]

I can use Filter and Map to filter out which does not contain 'Id' in Reference by the following:我可以使用 Filter 和 Map 通过以下方式过滤掉参考中不包含“Id”的内容:

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:所以从上面的数组中,我只需要去掉 Id 就可以得到:

// [123,133]

Using sub strings doesn't seem to work for me.使用子字符串似乎对我不起作用。

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

You could map the part with a regular expression for the digits and return a number.您可以 map 使用数字正则表达式的部分并返回一个数字。

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

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:如果您只是从Id:之后提取文本部分,则可以使用简单的数组操作:(四个字符,因此字符串中的索引最多为 3)和出现在数字之后的第一个逗号:

 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或者,您可以使用正则表达式来捕获和提取 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM