Open In App

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

Last Updated : 28 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. We can say, every variable is an object, in Javascript. 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)

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




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'));


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




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)


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' }
]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads