Different ways of swapping elements in a JS Array

Different ways of swapping elements in a JS Array

I was working on a UI where a group of cards are getting rendered based on the data that is present in the array & I've got a scenario where I have to swap my cards to match UI ( first card needs to be placed with 3rd card and vice versa). This is where I got pushed into a situation to swap elements in an array which contains the information to render the cards.

UI requirement #javascript

So at first I used a simple way of swapping elements in array by taking a temporary variable and assigned first element's value to it and then I set the value of third element to the first element. As a last step I assigned value of temporary variable to the third element. (PFB, code snippet)

method 1

And then I realised that there is far better approach compare to the above approach and that can be coded in single line. And that approach is array destructuring. I just created a new array containing both elements in a particular order, then assign it to a new array that contains both elements in the reversed order.

method 2

After trying the above approach I realised how could I forgot to use splice method to achieve the same result. BTW, splice method can be used to remove one or more element(s) from an array and replace the element(s) with any specified element. Below snippet explains the syntax of splice method.

No alt text provided for this image

splice method returns array of the deleted elements from the given array.

No alt text provided for this image

So, we can use this splice method for swapping of elements too.

No alt text provided for this image

In this article I tried to show 3 ways of swapping elements in an array but I personally like the 2nd method(array destructuring way).

Thanks for reading!!

#reactjs #frontenddeveloper #javascript #divami #webdevelopment

Gopal Gottumukkala

HIRING NOW!! React/Angular/Node UI/Full-Stack Developer (1-3/3-5/5-7 yrs)

2 年

Nice, Thank You for putting it together

要查看或添加评论,请登录

Siva Rajana的更多文章

  • Microfrontends

    Microfrontends

    GatsbyJS’s Founder Kyle Mathews made a profound observation recently, stating that, "Websites today are still made the…

    1 条评论
  • Web Accessibility: A Small Guide for Web Developers

    Web Accessibility: A Small Guide for Web Developers

    Web Accessibility refers to designing and developing websites, so that people with disabilities can use them without…

    1 条评论
  • Dancing with the Event Loop!!

    Dancing with the Event Loop!!

    Back in college, during our 3rd year of engineering, our batchmates were planning a farewell party for our…

    2 条评论
  • Closures and Cricket: A Lesson in Life and JavaScript

    Closures and Cricket: A Lesson in Life and JavaScript

    Once upon a time in the world of childhood, there was a passion that burned brighter than the scorching sun on a summer…

  • From Confusion to Clarity: Demystifying call, apply, and bind in JavaScript

    From Confusion to Clarity: Demystifying call, apply, and bind in JavaScript

    It had been a hectic day at the office, and I decided to sleep early. Suddenly, I felt like I couldn't breathe and was…

  • Unlocking Peak Performance in React Applications

    Unlocking Peak Performance in React Applications

    In today's web world, clients want more than just good-looking websites. They want websites that work fast and smoothly.

  • What is function currying in JavaScript ?

    What is function currying in JavaScript ?

    Function currying is a way to change a function that takes multiple arguments, like sum(a, b), into a series of…

    2 条评论
  • How is javascript code executed?

    How is javascript code executed?

    Javascript code will be executed in two phases whenever we try to run a javascript program. And in the first phase, the…

    4 条评论
  • How to sort an array of objects?

    How to sort an array of objects?

    Today I got a simple scenario where I have to sort objects in an array based on the value of a key present in each of…

  • Why I Stopped Using “&&” for Conditional Rendering in React

    Why I Stopped Using “&&” for Conditional Rendering in React

    I have been using the #javascript '&&' operator for conditionally rendering my react components. The value of the…

    2 条评论

社区洞察

其他会员也浏览了