Open In App

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 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:

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






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:

Syntax:

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

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




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

Syntax:

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

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




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' } ]

Article Tags :