How to Swap Array Object Values in JavaScript ?
Last Updated :
13 Aug, 2024
We have given the array of objects, and our task is to swap the values of the object keys present in the array of objects. Below is an example for a better understanding of the problem statement.
Example:
Input: arr = [{a: 1, b: 2}, {a:3, b: 4}]
Output: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]
Explnation: The values of the object keys a and b before swapping
were 1 and 2 respectively. But after swapping they gets interchanged.
1. Using Destructuring Assignment
This method uses the for loop to iterate over the array of objects, swapping the values of 'a' and 'b' properties in each object using the destructuring assignment. Once the swapping is done, we print the swapped values using the log() function.
Example: The below code uses the destructuring assignment to swap array object values using JavaScript.
JavaScript
// input arr
let arr = [{a: 1, b: 2}, {a: 3, b: 4}];
console.log("Before Swapping: ", arr);
// swapping using destructuring assignment
for (let i = 0; i< arr.length; i++) {
[arr[i].a, arr[i].b] = [arr[i].b, arr[i].a];
}
console.log("After Swapping: ", arr);
OutputBefore Swapping: [ { a: 1, b: 2 }, { a: 3, b: 4 } ]
After Swapping: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]
2. Using Temp Variable
This method uses the temporary variable to swap the values of the array of objects. The loop is used to go over the array of objects, swapping the values of 'a' and 'b' properties in each object using the temp variable.
Example: The below code uses the temp variable to swap the array of object values using JavaScript.
JavaScript
// input array of objects
let arr = [{a: 1, b: 2}, {a: 3, b: 4}];
console.log("Before Swapping: ", arr);
// swapping using temp var
for (let i = 0; i <arr.length; i++) {
let temp = arr[i].a;
arr[i].a = arr[i].b;
arr[i].b = temp;
}
// output array
console.log("After Swapping: ", arr);
OutputBefore Swapping: [ { a: 1, b: 2 }, { a: 3, b: 4 } ]
After Swapping: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]
3. Using Array.map() Method
This method uses the Array.map() method to create the new array by swapping the values of each key present in the object. The output consists of the swapped values printed using the log() function.
Example: The below code uses the Array.map() method to swap the array of object values using JavaScript.
JavaScript
// array of objects
let arr = [{a: 1, b: 2}, {a: 3, b: 4}];
console.log("Before Swapping: ", arr);
// swapping values using map()
arr = arr.map(obj => ({a: obj.b, b: obj.a}));
// output array
console.log("After Swapping: ", arr);
OutputBefore Swapping: [ { a: 1, b: 2 }, { a: 3, b: 4 } ]
After Swapping: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]
4. Using Array.forEach() Method
The Array.forEach() method can be used to iterate over the array of objects, then swap the values using the temp variable and the output consists of the modified swapped values of 'a' and 'b' properties.
Example: The below code uses the Array.forEach() method to swap the array of object values using JavaScript.
JavaScript
// arr of objs
let arr = [{a: 1, b: 2}, {a: 3, b: 4}];
console.log("Before Swapping: ", arr);
// swapping using forEach()
arr.forEach(obj => {
let temp = obj.a;
obj.a = obj.b;
obj.b = temp;
});
// output
console.log("After Swapping: ", arr);
OutputBefore Swapping: [ { a: 1, b: 2 }, { a: 3, b: 4 } ]
After Swapping: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]
5. Using Array.reduce() Method
In this approach, Array.reduce() is used to iterate over the array of objects and swap the values of specific properties. This method allows you to create a new array while performing the swap operation.
Example:
JavaScript
let arr = [{a: 1, b: 2}, {a: 3, b: 4}];
console.log("Before Swapping: ", arr);
let swappedArr = arr.reduce((acc, obj) => {
acc.push({ a: obj.b, b: obj.a });
return acc;
}, []);
console.log("After Swapping: ", swappedArr);
OutputBefore Swapping: [ { a: 1, b: 2 }, { a: 3, b: 4 } ]
After Swapping: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]
Similar Reads
How to Swap Two Array of Objects Values using JavaScript ? Swapping values between two arrays of objects is a common operation in JavaScript, there are multiple methods to swap values between arrays of objects efficiently. Swapping values between arrays can be useful in scenarios like reordering data, shuffling items, or performing certain algorithms. These
6 min read
How to Sort JSON Array in JavaScript by Value ? Sorting is the process of arranging elements in a specific order. In JavaScript, you can sort a JSON array by value using various methods as listed and discussed below with their practical implementation. Table of Content Using sort() FunctionUsing Array.prototype.reduce()Using Bubble SortUsing sort
3 min read
How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one
2 min read
How to Access Array of Objects in JavaScript ? Accessing an array of objects in JavaScript is a common task that involves retrieving and manipulating data stored within each object. This is essential when working with structured data, allowing developers to easily extract, update, or process information from multiple objects within an array.How
4 min read
How to Convert Object to Array in JavaScript? In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such
4 min read
How to Copy Array by Value in JavaScript ? There are various methods to copy array by value in JavaScript.1. Using Spread OperatorThe JavaScript spread operator is a concise and easy metho to copy an array by value. The spread operator allows you to expand an array into individual elements, which can then be used to create a new array.Syntax
4 min read
How to invert key value in JavaScript object ? JavaScript is a high-level, interpreted, and dynamically typed client-side scripting language. JavaScript is used to add dynamic features to the static HTML. Everything is an object in JavaScript. Objects in JavaScript can be declared using figure brackets {..} and the objects may comprise certain p
2 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
JavaScript - Use Arrays to Swap Variables Here are the different methods to swap variables using arrays in JavaScript1. Using Array DestructuringArray destructuring is the most efficient and modern way to swap variables in JavaScript. This method eliminates the need for a temporary variable.JavaScriptlet x = 5; let y = 10; [x, y] = [y, x];
3 min read
JavaScript - Swap Two Variables in JavaScript In this article, we will learn about swapping 2 variables with Various Approaches.Using destructing Assignment - Most UsedHere, we will be using a destructing Assignment. The destructing assignment makes it possible to unpack values from an array, object, or other properties into distinct variables.
3 min read