JavaScript Javascript filter array of objects skip empty string javascript filter array of objects,javascript filter array of objects

I am building a REST API in NodeJS. I am building the server-side pagination, sorting and filtering.

I have a simple implementation of the filter method. This works if the item does not contain any empty strings. However if the item contains an empty string, the .toString() method fails because item[filterBy] is null.

Cannot read property 'toString' of null

How can I update the filter function to simply return true if the item contains an empty string, to skip the comparison operations?

  // Apply filter to array of objects
  // TODO: Empty strings breaks the filter
  items = items.filter(item =>
    item[filterBy].toString().trim().toLowerCase().indexOf(filterValue.toLowerCase()) !== -1)
Answer:1

To skip the item contains an empty string, try this:

item[filterBy] && item[filterBy].toString()

So, your code will be:

items = items.filter(item =>
item[filterBy] && item[filterBy].toString().trim().toLowerCase().indexOf(filterValue.toLowerCase()) !== -1)
Answer:2

Perhaps something like this:

const filterValue = 'abc'
const filterBy = 'name'
const items = [
  {x: 1, name: 'abc'},
  {x: 2, name: 'def'},
  {x: 3, noname: 'def'},
  {x: 4, name: 'ABCDEF'},
]

const newItems = items.filter(item =>
    !(filterBy in item) || item[filterBy].toString().trim().toLowerCase().indexOf(filterValue.toLowerCase()) !== -1)
    
console.log(newItems)
Answer:3

Maybe:

items = items.filter(
    item => item[filterBy] && item[filterBy].toString ?
        item[filterBy].toString().trim().toLowerCase().indexOf(filterValue.toLowerCase()) !== -1 :
        false
)
Answer:4

flatMap() is .map() and .flat() combined. It can act like a reverse .filter() by directly returning or not returning values. See demo for details.

const items = [{
  id: 'a',
  pn: 'B',
  desc: ''
}, {
  id: 'b',
  pn: 'c',
  desc: 'd'
}, {
  id: 'c',
  pn: 'k',
  desc: null
}, {
  id: 'i',
  pn: 'k',
  desc: 2
},, {
  id: 'o',
  pn: 'x',
  desc: 3
}];

// Utility function that returns a 'friendlier' value
function clean(obj) {
  return obj == null ? false : Array.isArray(obj) ? obj : obj.constructor.name.toLowerCase() === "object" ? obj.toString().trim().toLowerCase() : typeof obj === "string" ? obj.toString().trim().toLowerCase() : Number(parseFloat(obj)) === obj ? obj : false;
}

// Filters by a single key and multiple values
let filterKey = clean('desc');
let filterVal = clean(['d', 2, 'f']);

/* It returns each value in a sub-array an empty array will remove
the value. In the final stage it flattens the array of arrays into a normal array
*/
let list = items.flatMap(item => {
  return Object.entries(item).flatMap(([key, value]) => filterKey === (clean(key)) && [...filterVal].includes(clean(value)) ? [clean(value)] :[]);
});

console.log(list);
Answer:5

I'm trying to break a foreach if a bool variable is valorized: $.each(GlobalVariables.availableServices, function(index, service) { if(state == true){ break; } for (var k = 0; k &...

I'm trying to break a foreach if a bool variable is valorized: $.each(GlobalVariables.availableServices, function(index, service) { if(state == true){ break; } for (var k = 0; k &...

  1. illegal break statement javascript
  2. illegal break statement foreach
  3. illegal break statement nodejs
  4. illegal break statement javascript foreach
  5. illegal break statement jquery
  6. illegal break statement foreach js
  7. illegal break statement c++
  8. illegal break statement switch
  9. illegal break statement protractor
  10. illegal break statement in node js
  11. illegal break statement angularjs
  12. illegal break statement angular.foreach

I want alert to pop if input letters length is equal to 0 but it is not working <div class="rame"> <input type=text placeholder="Name" style="position: relative; right:-750px" id="inp1"&...

I want alert to pop if input letters length is equal to 0 but it is not working <div class="rame"> <input type=text placeholder="Name" style="position: relative; right:-750px" id="inp1"&...

I am using only Javascript. I have the following string : ?pn1=age&pn2=name&pv1=12&pv2=alice What I need to do, is have the following outcome : age:12|name:alice I thought of a way to ...

I am using only Javascript. I have the following string : ?pn1=age&pn2=name&pv1=12&pv2=alice What I need to do, is have the following outcome : age:12|name:alice I thought of a way to ...

  1. sort array custom javascript
  2. sort array custom php
  3. sort array custom order
  4. sort array custom order swift
  5. sort array custom ruby
  6. sort array custom objects
  7. array sort custom comparator
  8. array sort custom function
  9. sort custom array swift
  10. array sort custom function php
  11. array sort custom java
  12. array sort custom
  13. array sort custom comparator java
  14. array sort custom comparator c#
  15. array sort custom c#
  16. sort custom array typescript
  17. arraylist custom sort
  18. javascript sort array custom comparator
  19. sort array of custom objects swift
  20. python sort array custom

I have a screen where I would like to calculate the next x and y based on angle. The first footstep is this example starts at Step 1. How can I calculate the next footstep where I want to increase ...

I have a screen where I would like to calculate the next x and y based on angle. The first footstep is this example starts at Step 1. How can I calculate the next footstep where I want to increase ...