-2

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).

5
  • 2
    Do you have any code that you've already tried, that you can share? Commented Jun 28, 2018 at 16:06
  • The actual code that I am working on is much more complex 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. Commented Jun 28, 2018 at 16:10
  • Possible duplicate of javascript .some function for object property 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. Commented Jun 28, 2018 at 16:25
  • @trincot I strongly disagree. Anyone coming 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 complex 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. Commented Jun 28, 2018 at 16:31

2 Answers 2

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 } ]
Sign up to request clarification or add additional context in comments.

Comments

2

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>

1 Comment

This is the best approach for a dynamic filter pattern. Thanks!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.