how can i match strings, if one of them are not complete equal?

bruce lee

i have two arrays and i want to verify to synchronic both, if in both existe the same name that should be stored in the array and the others no

for example: i need all the objects with name rea.jpg but in my array i have like this ['dsjajdsj...h2/rea.jpg']
this is the materials

[{name: 'hgb.jpg' }, { name: 'rea.jpg'}, {  name: 'ca.png' }]

the file extension is always the end of the string

i want to verify if exist an object in the names's array

const names = ['h2/rea.jpg']
const materials = [{
  name: 'hgb.jpg'
}, {
  name: 'rea.jpg'
}, {
  name: 'ca.png'
}]
const res = materials.filter(x => names.includes(x.name))
console.log('RES', res)

expected result shoud be [ {name: 'rea.jpg'}]

jarmod

Assuming this is Node.js, you can use the path library to convert the fully-qualified pathnames to simple filenames, then do your includes test. For example:

const path = require('path');

const pathnames = ['h2/rea.jpg'];
const filenames = pathnames.map((x) => path.basename(x));

const materials = [
  {
    name: 'hgb.jpg',
  },
  {
    name: 'rea.jpg',
  },
  {
    name: 'ca.png',
  },
];

const res = materials.filter((x) => filenames.includes(x.name));
console.log('RES', res);
// RES [ { name: 'rea.jpg' } ]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I check if two strings are equal in Java when either one of the strings can be a null?

Given multiple path templates, how can I merge them into a single complete one

How can I add strings and print them?

In pandas, how can I identify records that share a common value and replace the value of one of them to match the other?

How can I match overlapping strings with regex?

How can I check if strings match?

How can I partially match numbers to strings?

In Perl, how can I match one string against a list of other strings?

Using SQL how can I update field values of one column to match a different column's field value only if the values do NOT equal?

Comparing 2 seemingly equal strings returns false because one ends with char code 8291. How can I sanitize the string?

How can I match fuzzy match strings from two datasets?

How can I match a list of strings to another list of strings?

How can I count the number of equal words between two strings?

How can I make these visually identical strings computationally equal?

how can I combine multiple strings and output a comma between them?

Given two list of strings, how can I convert them into a into a dict?

How can I concatenate strings and use them as a list name?

How can I store strings with variables in them in another Java class?

How can I replace integers or strings that include space between them

How can I filter out strings that contain no numbers within them?

Regex: How to match all strings that start with one underscore and print them without the underscore?

How can I make a userform complete another one?

How can i make a subtraction if one cell in sheet is not equal to 0

How do I find strings that contain two strings and replace one of them

How can I match elements one-to-one?

How can I create many tables of the main one and store them?

how can i search for files and zip them in one zip file

How can I compare values in a dataframe row and select one of them?

Dynamic multiple plots, how can I call them one at a time?