How to Swap Array Object Values in JavaScript ?
source link: https://www.geeksforgeeks.org/how-to-swap-array-object-values-in-javascript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to Swap Array Object Values in JavaScript ?
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.
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]; } // output arr console.log( "After Swapping: " , arr); |
Before Swapping: [ { a: 1, b: 2 }, { a: 3, b: 4 } ] After Swapping: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]
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 arr of objs 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 arr console.log( "After Swapping: " , arr); |
Before Swapping: [ { a: 1, b: 2 }, { a: 3, b: 4 } ] After Swapping: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]
Using Array.map() Method
This method uses theArray.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
// arr of objs 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 arr console.log( "After Swapping: " , arr); |
Before Swapping: [ { a: 1, b: 2 }, { a: 3, b: 4 } ] After Swapping: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]
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); |
Before Swapping: [ { a: 1, b: 2 }, { a: 3, b: 4 } ] After Swapping: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]
Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK