Manipulating objects in array of objects in JavaScript


In the given problem statement we are asked to manipulate objects in an array of objects with the help of javascript functionalities. In Javascript we have some built−in functions called map(), reduce(), forEach() and filter() to manipulate the objects in an array. We can solve this problem with the help of these functions.

What is the manipulating object in an array ?

Let's understand the manipulation of objects in an array.

Manipulation means updating an array object, deleting an array object or adding an object in an array. In Javascript, there are several methods present to do these kinds of manipulations. The methods are as follows: slice(), filter(), find(), sort(), map(), reduce(), reverse(), some(), every() concat() and includes().

We will see the usage of some most useful functions in this article.

Logic for The Above Problem

The easiest way to manipulate the given string with the help of Javascript’s predefined functions.

So let's understand the logic of the problem given. The problem of manipulating the input string, we will update, delete, manipulate and replace some objects of the string. It depends on the specific use case there may be other methods or techniques that are more appropriate.

Below are some important techniques that are very useful to manipulate the object of an array:

Using map() function

The map method is used to create a new array populated with the result of calling a function on every item in the calling array.

Algorithm

Step 1: Declare an array with the name given users with two objects ‘name’ and ‘age’.

Step 2: Declare and initialize an additional array with the name updatedUsers following the first step. The map function will store the resultant array.

Step 3: Once the second step gets over then we will show the result of the updated array of objects.

Example

//define users array with two properties given
const users = [
  { name: 'Neha', age: 32 },
  { name: 'Ankit', age: 24 },
  { name: 'Babita', age: 41 },
];

//updating the users array
const updatedUsers = users.map(user => ({ ...user, isUser: false }));
console.log("Updated array is as follows")
console.log(updatedUsers);

Output

Updated array is as follows
[
  { name: 'Neha', age: 32, isUser: false },
  { name: 'Ankit', age: 24, isUser: false },
  { name: 'Babita', age: 41, isUser: false }
]

Using filter() function

The filter method will create a new array which will contain all items that pass the callback function's test.

Algorithm

Step 1: Declare an array with the name given users with two objects ‘name’ and ‘age’.

Step 2: Declare and initialize an additional array with the name youngUsers following the first step.

Step 3: Now forwarding to step 2, we will use the filter function to filter the users array as per the condition given in the call back function.

Step 4: Once the above step gets over then, show the result of the filtered array of objects.

Example

//define users array with two properties- name and age
const users = [
  { name: 'Neha', age: 32 },
  { name: 'Ankit', age: 24 },
  { name: 'Babita', age: 41 },
];

//create array for filtered array
const youngUsers = users.filter(user => user.age < 30);
console.log("Make a filter and show the data if age is less than 30:")
console.log(youngUsers);

Output

Make a filter and show the data if age is less than 30:
[ { name: 'Ankit', age: 24 } ]

Using find() function

Algorithm

Step 1: Declare an array with the name given users with two objects ‘name’ and ‘age’.

Step 2: Declare and initialize an additional array with the name updatedUsers following the first step. This is where the find function will be used to update the value of age as it satisfies the condition given inside the callback function.

Step 3: Now show the result of the updated array of objects.

Example

//define users array with name and age
const users = [
  { name: 'Neha', age: 32 },
  { name: 'Ankit', age: 24 },
  { name: 'Babita', age: 41 },
];

//updating a property value
const updatedUser = users.find(user => user.name === 'Neha');

if (updatedUser) {
  updatedUser.age = 35;
}
console.log("After updating age property:")
console.log(users);

Output

After updating age property:
[
  { name: 'Neha', age: 35 },
  { name: 'Ankit', age: 24 },
  { name: 'Babita', age: 41 }
]

Using slice() function

The slice() method will return a copy of a portion of an array into a newly created array object with the starting and ending index. It doesn't change the original array.

Algorithm

Step 1: Declare an array with the name given users.

Step 2: Now with the help of the slice method we will see a different array with different index values given inside the slice method.

Step 3: Now show the result of the updated array of objects.

Example

//define users array with names
const users = ['Neha', 'Ankit', 'Babita', 'Anmol'];

//show output with starting and ending index
console.log('Show output with starting and ending index');
console.log(users.slice(2));

console.log(users.slice(0, 2));

console.log(users.slice(1, 2));

Output

Show output with starting and ending index
[ 'Babita', 'Anmol' ]
[ 'Neha', 'Ankit' ]
[ 'Ankit' ]

Complexity

As we have seen different manipulation techniques to manipulate the given array called users. All the methods and techniques required O(n) time to complete execution and produce the required result. It is very important to note that this time complexity assumes that the number of objects in the input array is small as compared to the input size. If the input array size will be larger then we need to optimize the code for having better performance.

Conclusion

In this code we have implemented different manipulation techniques with the help of javascript’s array manipulation functions. And we have also seen that these methods are not very efficient in terms of larger array objects. And lastly calculated the time complexity for all the methods is O(n), where n is the size of the input objects.

Updated on: 23-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements