JavaScript – Array with() method

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

const people = [“Kristiyan”,”Emiliyan”, “Sally”];

people[2] = “Simon”;

Result: [“Kristiyan”, “Emiliyan”, “Simon”];

Description

The with() method changes the value o…


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

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

const people = ["Kristiyan","Emiliyan", "Sally"];

people[2] = "Simon";

Result: ["Kristiyan", "Emiliyan", "Simon"]; 

Description

The with() 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.

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

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

Example

const people = ["Kristiyan","Emiliyan", "Sally"];

people.with(2, "Simon");

Result: ["Kristiyan", "Emiliyan", "Simon"]; 

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 😍


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


Print Share Comment Cite Upload Translate Updates
APA

Kristiyan Velkov | Sciencx (2023-06-04T14:36:44+00:00) JavaScript – Array with() method. Retrieved from https://www.scien.cx/2023/06/04/javascript-array-with-method/

MLA
" » JavaScript – Array with() method." Kristiyan Velkov | Sciencx - Sunday June 4, 2023, https://www.scien.cx/2023/06/04/javascript-array-with-method/
HARVARD
Kristiyan Velkov | Sciencx Sunday June 4, 2023 » JavaScript – Array with() method., viewed ,<https://www.scien.cx/2023/06/04/javascript-array-with-method/>
VANCOUVER
Kristiyan Velkov | Sciencx - » JavaScript – Array with() method. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/06/04/javascript-array-with-method/
CHICAGO
" » JavaScript – Array with() method." Kristiyan Velkov | Sciencx - Accessed . https://www.scien.cx/2023/06/04/javascript-array-with-method/
IEEE
" » JavaScript – Array with() method." Kristiyan Velkov | Sciencx [Online]. Available: https://www.scien.cx/2023/06/04/javascript-array-with-method/. [Accessed: ]
rf:citation
» JavaScript – Array with() method | Kristiyan Velkov | Sciencx | https://www.scien.cx/2023/06/04/javascript-array-with-method/ |

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.