3

I have this array where i add values on click, but i want to check if value is already in array, if it is do nothing. I tried with indexOf but i get same result every time

this.fields.push(this.field);
      this.field = { value: '' };

3 Answers 3

8

In array of objects it is better to use Array.some()

this.users = [{id : 1, name : 'Jonh Doe'},{id : 2, name : 'Mary Jean'}]

if(!this.users.some(data => data.id === 1)){
    //don't exists       
}else{
    //exists because Jonh Doe has id 1
}

Source: https://www.samanthaming.com/

Sign up to request clarification or add additional context in comments.

2 Comments

Helped me a lot, but I see some small error there you have "!" inside if statement so in this it says "if not" so if in your case "id" exists it will fall to else not to if. But I have used your solution thank you :)
You are right, I already corrected it, it is because in my example I wanted to perform an action when it was not found in an array ☺
5

Are you determining if it's in the array by the value property? If so you can use Array.some().

var exists = this.fields.some(function(field) {
  return field.value === this.field.value
});

if (!exists) {
  this.fields.push(this.field);
}

Comments

2

This may be a detailed and easy solution.

In JavaScript or Jquery

//plain array
var arr = ['a', 'b', 'c'];
var check = arr.includes('a');
console.log(check); //returns true
if (check)
{
   // value exists in array
   //write some codes
}

// array with objects
var arr = [
      {x:'a', y:'b'},
      {x:'p', y:'q'}
  ];

// if you want to check if x:'p' exists in arr
var check = arr.filter(function (elm){
    if (elm.x == 'p')
    {
       return elm; // returns length = 1 (object exists in array)
    }
});

// or y:'q' exists in arr
var check = arr.filter(function (elm){
    if (elm.y == 'q')
    {
       return elm; // returns length = 1 (object exists in array)
    }
});

// if you want to check, if the entire object {x:'p', y:'q'} exists in arr
var check = arr.filter(function (elm){
    if (elm.x == 'p' && elm.y == 'q')
    {
       return elm; // returns length = 1 (object exists in array)
    }
});

// in all cases
console.log(check.length); // returns 1

if (check.length > 0)
{
   // returns true
   // object exists in array
   //write some codes
}

In Vue

<script>
    export default {
        data: function () {
            return {
               arr = [
                   {x:'a', y:'b'},
                   {x:'p', y:'q'}
                ],
             }
         },
        methods: {

            // assign this function to any event of any dom
            checkObjInArr = function(){

                     var check = this.arr.filter(function (elm) {
                         if (elm.x == 'a' && elm.y == 'M') {
                                 return elm;
                            }
                      });
                     console.log(check.length > 0); // returns 1
                     if (check.length > 0)
                        {
                            // returns true
                            // object exists in array
                            //write some codes
                         }
                },                
        },
     }
</script>

Comments

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.