Make immutable pop(), push(), shift(), unshift()?

We know very well how these functions work in Javascript. So, here I am going to write immutable implementations for the same functions as per my understanding. Please do comment your suggestions/concerns/questions. I will always happy to update this a…


This content originally appeared on DEV Community and was authored by DEV Community

We know very well how these functions work in Javascript. So, here I am going to write immutable implementations for the same functions as per my understanding. Please do comment your suggestions/concerns/questions. I will always happy to update this article.

function pop(arr) {
    let newArr = [...arr];
    newArr.length = arr.length - 1;
    return newArr;
}

function push(arr, ...item) {
    const newArr = [...arr];
    if (item.length >= 1) {
        for (let i = 0; i < item.length; i++) {
            newArr[newArr.length] = item[i];
        }
    }
    return newArr;
}

function shift(arr) {
    [a, ...b] = arr;
    return b;
}

function unshift(arr, ...item) {
    const newArr = [];
    if (item.length >= 1) {
        for (let i = 0; i < item.length; i++) {
            newArr[i] = item[i];
        }
    }
    newArr.push(...arr);
    return newArr;
}

Thank you! Happy Reading!

💎 Love to see your response

  1. Like - You reached here means. I think, I deserve a like.
  2. Comment - We can learn together.
  3. Share - Makes others also find this resource useful.
  4. Subscribe / Follow - to stay up to date with my daily articles.
  5. Encourage me - You can buy me a Coffee

Let's discuss further.

  1. Just DM @urstrulyvishwak
  2. Or mention
    @urstrulyvishwak

For further updates:

Follow @urstrulyvishwak


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-03-15T17:43:41+00:00) Make immutable pop(), push(), shift(), unshift()?. Retrieved from https://www.scien.cx/2022/03/15/make-immutable-pop-push-shift-unshift/

MLA
" » Make immutable pop(), push(), shift(), unshift()?." DEV Community | Sciencx - Tuesday March 15, 2022, https://www.scien.cx/2022/03/15/make-immutable-pop-push-shift-unshift/
HARVARD
DEV Community | Sciencx Tuesday March 15, 2022 » Make immutable pop(), push(), shift(), unshift()?., viewed ,<https://www.scien.cx/2022/03/15/make-immutable-pop-push-shift-unshift/>
VANCOUVER
DEV Community | Sciencx - » Make immutable pop(), push(), shift(), unshift()?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/15/make-immutable-pop-push-shift-unshift/
CHICAGO
" » Make immutable pop(), push(), shift(), unshift()?." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/15/make-immutable-pop-push-shift-unshift/
IEEE
" » Make immutable pop(), push(), shift(), unshift()?." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/15/make-immutable-pop-push-shift-unshift/. [Accessed: ]
rf:citation
» Make immutable pop(), push(), shift(), unshift()? | DEV Community | Sciencx | https://www.scien.cx/2022/03/15/make-immutable-pop-push-shift-unshift/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.