admin管理员组

文章数量:1336613

I have an array of objects which I am needing to check if a similar object already exists in the array. For example, is there an object that has the same make and model properties (Ford Focus):

{
  make: 'Ford',
  model: 'Focus',
  color: 'red',
  year: 2016
}

in this array?

[
  {
    make: 'Ford',
    model: 'Focus',
    color: 'blue',
    year: 2008
  },
  {
    make: 'Ford',
    model: 'Mustang',
    color: 'red',
    year: 2011
  },
  {
    make: 'Chevy',
    model: 'Malibu',
    color: 'blue',
    year: 2012
  },
  {
    make: 'Ford',
    model: 'Focus',
    color: 'black',
    year: 1999
  }
]

I'd prefer an ES6 method but can use lodash as well. Lodash has _.some but from what I can tell, it matches the whole object (not just the specific properties needed) or only one property. Additionally, I need something like _.pullAllWith where I can remove all objects that contain those specific properties (i.e. delete all objects that contain Ford Focuses).

I have an array of objects which I am needing to check if a similar object already exists in the array. For example, is there an object that has the same make and model properties (Ford Focus):

{
  make: 'Ford',
  model: 'Focus',
  color: 'red',
  year: 2016
}

in this array?

[
  {
    make: 'Ford',
    model: 'Focus',
    color: 'blue',
    year: 2008
  },
  {
    make: 'Ford',
    model: 'Mustang',
    color: 'red',
    year: 2011
  },
  {
    make: 'Chevy',
    model: 'Malibu',
    color: 'blue',
    year: 2012
  },
  {
    make: 'Ford',
    model: 'Focus',
    color: 'black',
    year: 1999
  }
]

I'd prefer an ES6 method but can use lodash as well. Lodash has _.some but from what I can tell, it matches the whole object (not just the specific properties needed) or only one property. Additionally, I need something like _.pullAllWith where I can remove all objects that contain those specific properties (i.e. delete all objects that contain Ford Focuses).

Share Improve this question asked Jun 28, 2018 at 16:05 Matt CampbellMatt Campbell 1714 silver badges14 bronze badges 5
  • 2 Do you have any code that you've already tried, that you can share? – Doug Commented Jun 28, 2018 at 16:06
  • The actual code that I am working on is much more plex than this example. I've read through various Lodash methods and the _.some and _.pullAllWith methods were the closest I could find to what I need. I also tried using .find and .findIndex but wasn't able to get it working correctly. – Matt Campbell Commented Jun 28, 2018 at 16:10
  • Possible duplicate of javascript .some function for object property – Alexander Commented Jun 28, 2018 at 16:11
  • If you have a problem with your code then ask specifically about that, posting your attempt. Just saying "I need something like" is not considered a good question. – trincot Commented Jun 28, 2018 at 16:25
  • @trincot I strongly disagree. Anyone ing here in the future will be able to follow this example and learn from it much more quickly and easily than if I posted a much longer, more plex example. There is no need to post something which will require a lot more explanation and a lot more stuff for everyone to go through. Plus, if I can use one of the answers and get it to work with my code, then that solves my problem. This is a much better way to go about posting questions. – Matt Campbell Commented Jun 28, 2018 at 16:31
Add a ment  | 

2 Answers 2

Reset to default 5

Javascript has some() and filter()

var cars = [
  {
    make: 'Ford',
    model: 'Focus',
    color: 'blue',
    year: 2008
  }, {
    make: 'Ford',
    model: 'Mustang',
    color: 'red',
    year: 2011
  }, {
    make: 'Chevy',
    model: 'Malibu',
    color: 'blue',
    year: 2012
  }, {
    make: 'Ford',
    model: 'Focus',
    color: 'black',
    year: 1999
  }
];

With some() you can find if some elements in the array contains that properties:

const containsFordFocus = cars.some( car =>  car.make === 'Ford' && car.model === 'Focus');
console.log(containsFordFocus); // true

With filter you can exclude some elements:

const filteredArray = cars.filter( car => car.make !== 'Ford' && car.model !== 'Focus');
console.log(filteredArray); // [ { make: 'Chevy', model: 'Malibu', color: 'blue', year: 2012 } ]

Create a function and use some and every, below works for whatever keys you pass in.

function exists(arr, obj, ...keys) {
    return arr.some(e => keys.every(k => e[k] && obj[k] && e[k] === obj[k]));
}

console.log(exists(arr, check, 'make', 'model'));
<script>
let arr = [
    {
        make: 'Ford',
        model: 'Focus',
        color: 'blue',
        year: 2008
    },
    {
        make: 'Ford',
        model: 'Mustang',
        color: 'red',
        year: 2011
    },
    {
        make: 'Chevy',
        model: 'Malibu',
        color: 'blue',
        year: 2012
    },
    {
        make: 'Ford',
        model: 'Focus',
        color: 'black',
        year: 1999
    }
]

let check = {
    make: 'Ford',
    model: 'Focus',
    color: 'red',
    year: 2016
};

</script>

本文标签: javascriptCheck Array of Objects by Multiple PropertiesStack Overflow