Filter with a part of input - javascript

I want a method to find, for example: if we have the name in input like "Gulliver", we need to find all objects in the array that contains that word.
I have this actually :
let data = this.articles.filter(d => d.name.toUpperCase() === this.search.toUpperCase())
But it's only finding the word who has been corresponding... between articles.name and my text input.
Thank you

You just need to use include or contains in your filter.
let data = this.articles.filter(d => d.name.toUpperCase().includes(this.search.toUpperCase()))

Related

Using maps over template literals

so I need to print a boolean value if the given string contains [[placeholder:var3]] but the var3 will be dynamically fed from an array as belwo:
const delmethod = ['all', 'email', 'sms', 'fax', 'voice', 'voicemail', 'pager', 'pagerTwoWay'];
let languages = organizationLocales.map(a => a.locale);
let variabless = alertDetails.variables?.map(k => k.name); languages && languages.length > 0 && languages.map(lang => { delmethod.map(i => {
if ( alertDetails.alertMessage?.[${lang}]?.[i]?.variabless?.some(el => [i] === [[placeholder:${el}]]) )
{ bodyContainsVariables = true; } }); })
I tried using map around the template literals but it is throwing me an error also tried like above eg but it checks only first value of the array, so can someone please help me solve this, I'd appreciate any help, thanks in advance.
updating the question with the actual PS. organizationLocales is an array and alertDetails is an object that has an variables array, Delmethod is an array is being used to check the different properties dynamically
[[placeholder:${variabless}]] yields [[placeholder:var1,var2,var3]]. It doesn't check the first variable. And that's not how Array.string.includes is supposed to be used. For checking exact match use the === operator.
In case you want to check if any of the array elements match the string you can use the Array.prototype.some method which returns a boolean value.
let any = variabless.some(el => s === `[[placeholder:${el}]]`)
If you want to find the element that matches with the string use the Array.prototype.find function.
You need to tell the index inside template literals becasue variabless is an array like this:
console.log(s.includes(`[[placeholder:${variabless[2]}]]`));
I hope it helps you.
To match against any value in variabless, you can test against a regular expression:
console.log(new RegExp(
`\\[\\[placeholder:(${variabless.join('|')})\\]\\]`
).test(s));
you're currently passing the whole array in
s.includes([[placeholder:${variabless}]])
and that's won't work, you need to specify which elemnt of the variables array you want to inject to your string which is element indexed with 2 so it should be like that
s.includes([[placeholder:${variabless[2]}]])

Filter an array from another array of elements and return specific positions

I'm finally learning better methods for my JS. I'm trying to find a way to go faster than I do so far :
In my code, I have two arrays :
one with unique keys in first position
one with those keys in first position but not unique. There are multiple entries with a certain value I want to filter.
The thing is I don't want to filter everything that is in the second array. I want to select some positions, like item[1]+item[5]+item[6]. What I do works, but I wonder if there isn't a faster way to do it ?
for (let i=0;i<firstArrayOfUniques.length;i++){
const findRef = secondArrayOfMultiple
.filter(item => item[0]==firstArrayOfUniques[i][0]);
// Afterwards, I redo a map and select only the second element,
//then I join the multiple answers
// Is there a way to do all that in the first filter?
const refSliced = findRef.map(x=>x[1]);
const refJoin = refSliced.join(" - ");
canevasSheet.getRange(1+i,1).setValue(refJoin);
}
The script snippet you quote will spend almost all of its running time calling the Range.setValue() method. It gets called separately for every data row. Use Range.setValues() instead, and call it just once, like this:
function moot(firstArrayOfUniques, secondArrayOfMultiple) {
const result = firstArrayOfUniques.map(uniqueRow =>
secondArrayOfMultiple
.filter(row => row[0] === uniqueRow[0])
.map(row => row[1])
.join(' - '));
canevasSheet.getRange(1, 1, result.length, result[0].length).setValues(result);
}
See Apps Script best practices.

react js get value from object based on key name

my return object looks like this. I would like to extract the value if code==="targetDate". Could someone please help.
Use the JavaScript array find function:
const matched = this.props.notifications.data.find(item => item.value === "targetDate");

How do I see if a character exists within each instance of the array as I type within an array of objects?

I've been struggling with this issue so I thought I'd come here to find the fix. I'm trying to filter my objects that are in an array to only show the ones that have for example "h" in them.
This is how my array of objects is being constructed:
Object.keys(data.jello).forEach(function (id, SomeSearchValue) {
var jello = data.jello[id];
array.push({
id: jello.id,
name: jello.name,
type: 'foo',
});
}
For example, let's say my array consists of this for names.
array[blue, red, green, apple, abigail, alien]
So, I have an array of objects. In a search bar I type "a" and my array results are then all objects that have a letter 'a' in the name. so...
array[abigail, alien]
Then i type a "b" so my search is now "ab" and my array is ...
array[abigail]
So what would my code to implement this using jQuery?
The part I'm stuck on most is the actual searching as the user types without using ajax.
I'm using jQuery. Ive tried things like jQuery.grep(), inArray() but cant seem to search includes. I've also tried array.includes(). Thanks for the help!
Use Array#filter().
jQuery $.grep() is basically the same thing but also works on jQuery objects.
Following assumes you only want to search within name property and will accept the term found in any part of the name rather than just at beginning
const data = [{name:'blue'}, {name:'red'}, {name:'green'}, {name:'apple'}, {name:'abigail'}, {name:'alien'}];
const term ='ab';
const filtered = data.filter(({name}) => name.toLowerCase().includes(term.toLowerCase()))
// or use startsWith() to only match beginning of string
console.log(filtered)

In casesensitive search by filter and include

I have the following line of code
let searchedListItems = searchableItems.filter(searchableItems => searchableItems.includes(this.state.userSearch))
where searchableItems is an array of strings
I want to get alle the values in the array into let searchedListItems and it all works - sort of...
My problem is that i need the include to not be case sensitive. Currently Test and test doesn't match up, but i need them to
You could use toLowerCase with the array elements and inputs:
let searchedListItems = searchableItems.filter(searchableItems => searchableItems.toLowerCase().includes(this.state.userSearch.toLowerCase()))

Categories

Resources