Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for JavaScript - Array with() method
Kristiyan Velkov
Kristiyan Velkov

Posted on • Edited on

     

JavaScript - Array with() method

Well, forget about the old and ugly way to update the value of some array.

constpeople=["Kristiyan","Emiliyan","Sally"];people[2]="Simon";Result:["Kristiyan","Emiliyan","Simon"];
Enter fullscreen modeExit fullscreen mode

Description

Thewith() method changes the value of a given index in the array, returning a new array with the element at the given index replaced with the given value. The original array is not modified. This allows you to chain array methods while doing manipulations.

Thewith() method never produces a sparse array. If the source array is sparse, the empty slots will be replaced with undefined in the new array.

Thewith() method is generic. It only expects the this value to have a length property and integer-keyed properties.


Example

constpeople=["Kristiyan","Emiliyan","Sally"];people.with(2,"Simon");Result:["Kristiyan","Emiliyan","Simon"];
Enter fullscreen modeExit fullscreen mode

Browser's compatibility

Image description


Image description

linkedin


Image description

If you like my work and want to support me to work hard, please donate via:

Revolut website payment or use the QR code above.

Thanks a bunch for supporting me! It means a LOT 😍

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jjaijg profile image
Jai Ganesh J
Frontend developer | ReactJS | Always explore new things
  • Location
    Tamilnadu, India
  • Education
    B.E EEE at Mepco Schlem Engg college, Sivakasi, Tamilnadu, India
  • Work
    Sr. Developer at Cognizant
  • Joined

What is the browser compatibility for this method?

CollapseExpand
 
kristiyanvelkov profile image
Kristiyan Velkov
Front-end Advocate || Mentor || Leader || Blogger || Book Author
  • Location
    Bulgaria, Sofia
  • Education
    Law
  • Work
    Tech Lead | JavaScript Domain knowledge lead
  • Joined

Very good question. Thank you!
I will update the article with this information.

Best regards,
Kristiyan

CollapseExpand
 
eboye profile image
eboye
Used to be a graphic designer that switched to UI/UX ... but coding is much more fun :)
  • Location
    Novi Sad
  • Work
    Full Stack developer at NoCopyPaste
  • Joined

No Firefox, no ♥️

CollapseExpand
 
kristiyanvelkov profile image
Kristiyan Velkov
Front-end Advocate || Mentor || Leader || Blogger || Book Author
  • Location
    Bulgaria, Sofia
  • Education
    Law
  • Work
    Tech Lead | JavaScript Domain knowledge lead
  • Joined

yeah, so sad!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Front-end Advocate || Mentor || Leader || Blogger || Book Author
  • Location
    Bulgaria, Sofia
  • Education
    Law
  • Work
    Tech Lead | JavaScript Domain knowledge lead
  • Joined

More fromKristiyan Velkov

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp