How Check if object value exists not add a new object to array using JavaScript ?
Last Updated :
28 Apr, 2023
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' }
]
Similar Reads
How to Append an Object as a Key Value in an Existing Object in JavaScript ? In JavaScript, An object is a key-value pair structure. The key represents the property of the object and the value represents the associated value of the property. In JavaScript objects, we can also append a new object as a Key-value pair in an existing object in various ways which are as follows.
3 min read
How to Create an Array of Object Literals in a Loop using JavaScript? Creating arrays of object literals is a very frequent activity in JavaScript, as often when working with data structures, several objects are needed. This article will allow your hand through different ways of coming up with an array of object literals in a loop in JavaScript. Here are different app
5 min read
How to Move a Key in an Array of Objects using JavaScript? The JavaScript array of objects is a type of array that contains JavaScript objects as its elements.You can move or add a key to these types of arrays using the below methods in JavaScript:Table of ContentUsing Object Destructuring and Map()Using forEach() methodUsing for...of LoopUsing reduce() met
5 min read
How to Check a Key Exists in JavaScript Object? Here are different ways to check a key exists in an object in JavaScript.Note: Objects in JavaScript are non-primitive data types that hold an unordered collection of key-value pairs. check a key exists in JavaScript object1. Using in Operator The in operator in JavaScript checks if a key exists in
2 min read
How to Check an Object is Empty using JavaScript? These are the following ways that can be used to Check an Object is Empty using JavaScript: 1. Using Object.keys() Method - Mostly usedThe Object.keys() method returns an array that contains the property names of an object. If the length of array is 0, then object is empty.JavaScriptlet obj = {}; if
2 min read
How to filter out the non-unique values in an array using JavaScript ? In JavaScript, arrays are the object using the index as the key of values. In this article, let us see how we can filter out all the non-unique values and in return get all the unique and non-repeating elements. These are the following ways by which we can filter out the non-unique values in an arra
4 min read
How to Find Property Values in an Array of Object using if/else Condition in JavaScript ? Finding property values in an array of objects using if/else condition is particularly useful when there is a collection of objects. Table of ContentUsing Array Find MethodUsing Array Filter MethodUsing For Of LoopUsing Array Map MethodUsing Array Reduce MethodUsing Array Some MethodUsing Array Find
5 min read
How to add a property to a JavaScript object using a variable as the name? In JavaScript, you can dynamically add a property to an object using a variable as the name. This allows you to create flexible and dynamic objects based on runtime values, enhancing code reusability and adaptability. There are several methods that can be used to add a property to a JavaScript objec
2 min read
How to Convert Array of Objects into Unique Array of Objects in JavaScript ? Arrays of objects are a common data structure in JavaScript, often used to store and manipulate collections of related data. However, there are scenarios where you may need to convert an array of objects into a unique array, removing any duplicate objects based on specific criteria. JavaScript has v
8 min read
How to check whether an object exists in javascript ? Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value. This helps avoid errors when accessing or manipulating objects that may be undefined, null, or not initialized properly.Here we have some common approaches to
3 min read