JavaScript Filter array of objects by multiple values filter array objects javascript,filter array objects angular 6,filter

I want to be able to create a new array of objects by filtering one by multiple search terms

Example:

  const arr = [
  {
      'city': 'Atlanta',
      'state': 'Georgia'
  },
  {
      'city': 'Chicago',
      'state': 'Illinois'
  },
  {
      'city': 'Miami',
      'state': 'Florida'
  }
]

const searchTerms = ['Georgia', 'Florida']

I would like to be able to filter it like this:

arr.filter(obj => obj['state'].includes(searchTerms))

I've found that entering one string with the .includes works, but not an array. I'm open to different logic or even a third party library like lodash or something. I would like to return a new array of objects with only the states that are in the searchterms array

Answer:1

You should call searchTerms.includes on obj.state and not the other way around. So it becomes:

let result = arr.filter(obj => searchTerms.includes(obj.state));

Which means filter out objects that have thier state property included in the array searchItems.

Example:

const arr = [{'city': 'Atlanta', 'state': 'Georgia'}, {'city': 'Chicago', 'state': 'Illinois'}, {'city': 'Miami', 'state': 'Florida'}];

const searchTerms = ['Georgia', 'Florida'];

let result = arr.filter(obj => searchTerms.includes(obj.state));

console.log(result);
Answer:2

If you're interested in a solution using Ramda:

const cities = [
  { 'city': 'Atlanta',
    'state': 'Georgia' },
    
  { 'city': 'Chicago',
    'state': 'Illinois' },
    
  { 'city': 'Miami',
    'state': 'Florida' } ];
    

const findCities = (search, cities) => {
  const predicate = R.flip(R.includes)(search);
  return R.filter(R.compose(predicate, R.prop('state')), cities);
};

console.log(
  findCities(['Georgia', 'Florida'], cities)
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
Answer:3

In React, which one of the following ways is the best way to define action types? First way: Defining actions using strings like the following: const actionCreatorExample = (value) => { ...

In React, which one of the following ways is the best way to define action types? First way: Defining actions using strings like the following: const actionCreatorExample = (value) => { ...

  1. react best practices
  2. react best practices 2019
  3. react best practices 2018
  4. react best ui framework
  5. react best tutorial
  6. react best practices folder structure
  7. react best practices github
  8. react best libraries
  9. react best book
  10. react best ide
  11. react best folder structure
  12. react best css framework
  13. react best form library
  14. react best components
  15. react best course
  16. react best ui
  17. react best practises
  18. react best place to fetch data
  19. react best chart library
  20. react best router

Consider a custom directive in angularjs: .directive('myDir', function() { return { restrict: 'E', template: "..." } }) As far as I see the tag <my-dir></myDir> has no ...

Consider a custom directive in angularjs: .directive('myDir', function() { return { restrict: 'E', template: "..." } }) As far as I see the tag <my-dir></myDir> has no ...

  1. deal with custom
  2. deals custom woodworks
  3. how to deal with custom officer
  4. custom deal with it gif

let suppose I have an array var a = [1,2,3,4] and now I want to print this array from the back side like 4,3,2,1 I know this can we achieve by this for(let i=a.length; i>= 0; i--) { console....

let suppose I have an array var a = [1,2,3,4] and now I want to print this array from the back side like 4,3,2,1 I know this can we achieve by this for(let i=a.length; i>= 0; i--) { console....

  1. print array element python
  2. print array element bash
  3. print array element php
  4. print array elements in java
  5. print array elements in c
  6. print array elements javascript
  7. print array elements in reverse order java
  8. print array elements in shell script
  9. print array elements perl
  10. print array elements using recursion java
  11. print array elements in reverse order c++
  12. print array elements in new line javascript
  13. print array elements in reverse order javascript
  14. print array element matlab
  15. print array elements in ascending order
  16. print array elements using recursion in c
  17. print array elements in one line java
  18. print array elements using pointers
  19. print array elements in scala
  20. print array elements in java 8

I've written the following code that generates a select list. However, the part where it's supposed to check if this.id equals the same as the existing_code and put the selected value in doesnt seem ...

I've written the following code that generates a select list. However, the part where it's supposed to check if this.id equals the same as the existing_code and put the selected value in doesnt seem ...

  1. javascript foreach select options
  2. javascript foreach select