JavaScript Can we use .filter() function with conditional rules for the searched properties? filter function with multiple criteria,filter function with lambda python,

Let's assume that we have the following .filter function:

search(searchQuery: string) {
  let results = myArray.filter(item => 
    item.title.toLowerCase().includes(searchQuery) ||
    item.description.toLowerCase().includes(searchQuery)
  );

  return results;
}

For this example we can consider that item doesn't have a description property always but only some times. In this case function will fail with an error:

ERROR TypeError: Cannot read property 'toLowerCase' of undefined

How can we can still search in the array and include the description property in the search only if it exists? Is this possible within the .filter() function?

Answer:1

You could take a default value.

Either directly

(item.description || '').toLowerCase().includes(searchQuery)

or take a destructuring with a default values.

results = myArray.filter(({ title = '', description = '' }) => 
    title.toLowerCase().includes(searchQuery) ||
    description.toLowerCase().includes(searchQuery)
);
Answer:2

You can do something like this:

const myArray = [
    {
        title: 'Test 123',
    },
    {
        title: 'Test 123',
        description: 'test',
    },
    {
        title: 'What',
    },
    {
        title: 'Fix',
        description: '456123',
    },
];

const search = (text) => {
    let results = myArray.filter(item => {
        if (item.description) {
            return item.title.toLowerCase().includes(text) || item.description.toLowerCase().includes(text);
        }
    });
  
    return results;
}

const results = search('test');

console.log(results);
Answer:3

My task was to create a while loop that counts the number of letter characters in a sentence written by the User. This function would only count the number of letter characters, no special characters. ...

My task was to create a while loop that counts the number of letter characters in a sentence written by the User. This function would only count the number of letter characters, no special characters. ...

  1. javascript character letter
  2. javascript check character letter
  3. javascript regex first character letter

So I'm basically making a grid where a single click is a circle and a double click is a square. However, if I go back to an already clicked box and make a single or double click, it changes the shape ...

So I'm basically making a grid where a single click is a circle and a double click is a square. However, if I go back to an already clicked box and make a single or double click, it changes the shape ...

How to sort the array of json data having letters and digits? JS: function sortOn(property) { return function (a, b) { if (a[property] < b[property]) { return -...

How to sort the array of json data having letters and digits? JS: function sortOn(property) { return function (a, b) { if (a[property] < b[property]) { return -...

I am trying to write a debounce function in typescript, but am unsure of what type to set a variable that is assigned to setTimeout. My code looks like this: function debounced(func: () => void, ...

I am trying to write a debounce function in typescript, but am unsure of what type to set a variable that is assigned to setTimeout. My code looks like this: function debounced(func: () => void, ...

  1. what type should i make silvally
  2. what type should infj date
  3. what type should i use against fighting
  4. what type should i date
  5. what type should intj date
  6. what type should intp date
  7. what type should i make type null
  8. what type should silvally be
  9. what type should type 9 date
  10. what type should
  11. what should type 2 diabetics eat
  12. what should type 1 diabetics eat
  13. what should type o blood eat
  14. what should type a blood eat
  15. what should type b blood eat
  16. what should type a positive blood eat
  17. what should type ab blood eat
  18. what personality type should i date
  19. what enneagram type should i date
  20. what type dog should i get