Skip to content
Related Articles
Open in App
Not now

Related Articles

How Check if object value exists not add a new object to array using JavaScript ?

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 28 Nov, 2022
Improve Article
Save Article

In this tutorial, we need to check whether an object exists within a JavaScript array of objects and if they are not present then we need to add a new object to the array.  For example, if we have an array of objects like the following.

obj =   { id: 1, name: ''Geeks1" }, 
        { id: 2, name: '"Geeks2"}, 
        { id: 3, name: "Geeks3" }

Approach: We use the some() function method in JavaScript. The some() method is used to check whether any array elements pass a test as provided by callback functions. For empty array elements, this method does not work and this method does not modify the original array.

Syntax:

obj.some(function(value, index)

Parameters:

  • value: The value of the current element.
  • index: The index of the current element.

Example 1: The following code checks whether the object value exists in an array. With the usage of some() function, we check whether each object in the array ‘obj’ contains a property with a specific value i.e it has an id and a name in this case. The “obj.some” takes a function parameter “gfg” and returns “true” if the desired object is found in the array (obj). The checkName() function with the name argument with the value of the object returns a boolean value.

Javascript




<script>
    var obj = [{ id: 1,  name: "Geeks1" }, 
                { id: 2, name: "Geeks2"}, 
                { id: 3, name: "Geeks3" }]
  
    function checkName(name) {
        return obj.some(function(gfg) {
          return gfg.name === name;
        }); 
    }
  
    console.log(checkName('Geeks1'));
    console.log(checkName('Geeks4')); 
</script>

Output:

true
false

Example 2: The following example demonstrates adding an object to the array if it is not present. 

To add a new object, we push a new element in the array using obj.length+1. The length is referred to the property of an object which returns the length of the array. push() method appends the object to the array and adds the element to the end of the object array. We have a function “checkName” through which we pass the name as a parameter and once the name matches that of the object, it returns “true”. In the addObj() function, if any new name is encountered the push() function is used for inserting the element in the subsequent next id.

Javascript




<script>
    var obj = [{ id: 1, name: "Geeks1" },
    { id: 2, name: "Geeks2" },
    { id: 3, name: "Geeks3" }]
      
    function checkName(name) {
        return obj.some(function (gfg) {
            return gfg.name === name;
        });
    }
      
    function addObj(name) {
        if (checkName(name)) {
            return false;
        }
        obj.push({ id: obj.length + 1, name: name });
        return true;
    }
    console.log("Adding 'Geeks1' which is not present in the obj")
    addObj('Geeks1')
    console.log(obj)
    console.log("*******************************************")
    console.log("Adding 'Geeks4' which is not present in the obj")
    addObj('Geeks4')
    console.log(obj)
</script>

Output:

Adding 'Geeks1' which is not present in the obj
[
      { id: 1, name: 'Geeks1' },
      { id: 2, name: 'Geeks2' },
      { id: 3, name: 'Geeks3' }
]
*******************************************
Adding 'Geeks4' which is not present in the obj
[
      { id: 1, name: 'Geeks1' },
      { id: 2, name: 'Geeks2' },
      { id: 3, name: 'Geeks3' },
      { id: 4, name: 'Geeks4' }
]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!