最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filter array of objects by multiple values - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question asked Dec 2, 2018 at 0:29 Devon NorrisDevon Norris 1802 silver badges12 bronze badges 1
  • 1 you should search for state in searchTerms not the other way. – Rahim Commented Dec 2, 2018 at 0:35
Add a ment  | 

3 Answers 3

Reset to default 5

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

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.pose(predicate, R.prop('state')), cities);
};

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

Another approach you could take here is to utilize Map which would have consistent retrieval times:

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

const searchMap = arr.reduce((r,c) => (r.set(c.state, c),r), new Map())

console.log(searchTerms.map(x => searchMap.get(x)))

发布评论

评论列表(0)

  1. 暂无评论