bruce lee
bruce lee

Reputation: 13

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

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'}]

Upvotes: 0

Views: 82

Answers (5)

Maik Lowrey
Maik Lowrey

Reputation: 17556

If you like it oldscool, then with two loops and Regex.

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

  const res = [];

materials.forEach((x) => {
    let search = x.name    
    let reg = new RegExp(search, 'g');
    let match = '';
    names.forEach((n) => {
      match = n.match(reg);
    })
    if(match) {
      res.push(x)
    }  
})

console.log('RES', res)

Upvotes: 1

jarmod
jarmod

Reputation: 78583

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' } ]

Upvotes: 1

Ran Turner
Ran Turner

Reputation: 17946

Use some inside of the filter to acheive this (note, this will run in O(n^2)).

const names = ['h2/rea.jpg']
const materials = [{
  name: 'hgb.jpg'
}, {
  name: 'rea.jpg'
}, {
  name: 'ca.png'
}]
const res = materials.filter(x => names.some(y => y.endsWith('/' + x.name)))

console.log('RES', res)

Upvotes: 1

sin tribu
sin tribu

Reputation: 1180

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

But if names is long and you don't want to iterate over it on every element of materials follow the other suggestion and transform names to only include the filename.

const fnames = names.map(ni => ni.split('/')[ni.split('/').length-1])
const res = materials.filter(x => names.includes(x.name))
console.log(res)

Upvotes: 1

ProDec
ProDec

Reputation: 5410

Use Array#some to pass in a custom compare function.

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

Upvotes: 1

Related Questions