Open In App

How to Swap Two Array of Objects Values using JavaScript ?

Last Updated : 19 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 are the following approaches:

Approach 1: Using a Temporary Variable

swapping values between two arrays of objects using a temporary variable involves the following steps:

  • Create a temporary variable to store the value of the array during the swap.
  • Assign the value of the first array of object to the temporary variable.
  • Assign the value of the second array of object to the position originally held by the first array of object.
  • Assign the value stored in the temporary variable to the position held by the second array of object.
  • Repeat the assignments for each corresponding pair of object in the arrays.
  • After completing the swap for all object in the arrays, return ‘true’ to indicate that the swapping was successful.

Example: This example shows the implementation of the above-explained approach.

Javascript




function fun(arrayA, arrayB) {
 
    // Arrays must have the same length
    if (arrayA.length !== arrayB.length) {
        return false;
    }
 
    for (let i = 0; i < arrayA.length; i++) {
        let tempVariable = arrayA[i];
        arrayA[i] = arrayB[i];
        arrayB[i] = tempVariable;
    }
    // Swapping arrays successful
    return true;
}
 
let arrayA = [{ name: "geek" },
{ name: "geek3" }];
let arrayB = [{ name: "geek1" },
{ name: "geek7" }];
 
let result = fun(arrayA, arrayB);
if (result) {
    console.log(arrayA);
    console.log(arrayB);
}
else {
    console.log("The length of an array must be the same");
}


Output

[ { name: 'geek1' }, { name: 'geek7' } ]
[ { name: 'geek' }, { name: 'geek3' } ]

Approach 2: Using Destructuring Assignment

swapping values between two arrays of objects using a destructuring assignment involves the following steps:

  • Use array destructuring assignment to simultanously extract elements from both arrays into variables
  • The elements from ‘arrayA’ are assigned to ‘arrayB’ and vice versa.
  • Repeate this destructuring assignment for each pair of correspoding element in the array.
  • After completing the swap for all object in the arrays, return ‘true’ to indicating that the swapping was successful.

Syntax:

[arrayA[i], arrayB[i]] = [arrayB[i], arrayA[i]];

Example: This example shows the implementation of the above-explained approach.

Javascript




function swapArraysUsingDestructuring(arrayA,arrayB) {
 
    // Arrays must have the same length
    if (arrayA.length !== arrayB.length) {
        return false;
    }
 
    for (let i = 0; i < arrayA.length; i++) {
        [arrayA[i], arrayB[i]] = [arrayB[i], arrayA[i]];
    }
 
    // Swapping arrays successful
    return true;
}
 
 let arrayA = [1, 2, 3];
 let arrayB = [5, 6, 7];
     
let result=swapArraysUsingDestructuring(arrayA,arrayB);
    if(result)
    {
         console.log(`Array A: ${arrayA}`); //Output -> arrayA = [5, 6, 7]
        console.log(`Array B: ${arrayB}`);//Output -> arrayB = [1, 2, 3]
    }
    else
    {
        console.log("The length of an array must be the same");
    }


Output

Array A: 5,6,7
Array B: 1,2,3

Approach 3: Using Array.prototype.splice() Method

  • Use the Splice() method to remove element from one array and insert them into another array at specified position (index).
  • Repeat this splicing operation for each pair of corresponding elelment in the arrays.
  • After completing the swap for all object in the arrays, return ‘true’ to indicating that the swapping was successful.

Syntax:

arrayA.splice(i, 1, ...arrayB.splice(i, 1, arrayA[i])[0]);

Example: This example shows the implementation of the above-explained approach.

Javascript




function fun(arrayA, arrayB) {
 
    // Arrays must have the same length
    if (arrayA.length !== arrayB.length) {
        return false;
    }
 
    for (let i = 0; i < arrayA.length; i++) {
        arrayA.splice(i, 1, arrayB.splice(i, 1,
            arrayA[i])[0]);
    }
 
    // Swapping arrays successful
    return true;
}
 
let arrayA = [{ name: "Pratik" },
{ name: "Mahesh" }];
let arrayB = [{ name: "Rohit" },
{ name: "Rahul" }];
 
let result = fun(arrayA, arrayB);
if (result) {
    console.log("arrayA =", arrayA);
    console.log("arrayB =", arrayB);
}
else {
    console.log("The length of an array must be the same");
}


Output

arrayA = [ { name: 'Rohit' }, { name: 'Rahul' } ]
arrayB = [ { name: 'Pratik' }, { name: 'Mahesh' } ]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads