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
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);
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>
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) => { ...
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 ...
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....
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 ...