Search Array with variable case-insensitive javascript

2

I need to search an array with values of type:

equipamentos = [{marca: 'Nomedamarca1', modelo: 'Modelo1'},
                {marca: 'Nomedamarca2', modelo: 'Modelo2'},
                {marca: 'AlgumNomeComCase', modelo: 'Modelo3'},
                {marca: 'OutroNomeComCase', modelo: 'Modelo4'}]

But in my input, I want to accept low-case values, for example:

When the user types "name", the search should return: "[Namename1, Namename2, SomeCompanyName, OtherCompanyName]"

When the user types "somename", the search should return: "[SomeCompanyName]"

I'm using the filter and match function, however it's not case-insensitive.

ShowDShowDropModelrep(value) {

const marca = this.modelsfull.filter( obj => obj[ 'marca' ].match( value ) );
const modelo = this.modelsfull.filter( obj => obj[ 'modelo' ].match( value ) );
Object.keys(marca).forEach(function(key) { modelo[key] = marca[key]; });
this.marcamodelo = modelo; }

Reading the match function documentation ( mozilla doc , W3school ) tried unsuccessfully the following:

value = '/' + value + '/gi';

One of the possible workarounds would be to apply .toLowerCase () to all positions in the array, but I need to keep the original data with the original case.

    
asked by anonymous 26.06.2018 / 17:00

1 answer

1

On wallace's comment, I was able to solve my problem this way:

ShowDShowDropModelrep(value) {
   const marca = this.modelsfull.filter( obj => obj[ 'marca' ].toLowerCase().match( value ) );
   const modelo = this.modelsfull.filter( obj => obj[ 'modelo' ].toLowerCase().match( value ) );
   Object.keys(marca).forEach(function(key) { modelo[key] = marca[key]; });
   this.marcamodelo = modelo;}
    
26.06.2018 / 17:39