Skip to content

Commit

Permalink
link at and with together (mdn#29099)
Browse files Browse the repository at this point in the history
* add `at` to `with`'s "See also" section

* add `with` to `at`'s "See also" section

* fix jsxref syntax error

* mdn#29099 (comment)

* mdn#29099 (comment)

* Fix see also

* Apply suggestions from code review

---------

Co-authored-by: Joshua Chen <[email protected]>
  • Loading branch information
Rudxain and Josh-Cena authored Sep 15, 2023
1 parent 882679e commit a815a95
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ The `at()` method is equivalent to the bracket notation when `index` is non-nega

The usual practice is to access {{jsxref("Array/length", "length")}} and calculate the index from that — for example, `array[array.length - 1]`. The `at()` method allows relative indexing, so this can be shortened to `array.at(-1)`.

By combining `at()` with {{jsxref("Array/with", "with()")}}, you can both read and write (respectively) an array using negative indices.

The `at()` method is [generic](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#generic_array_methods). It only expects the `this` value to have a `length` property and integer-keyed properties.

## Examples
Expand Down Expand Up @@ -110,5 +112,6 @@ console.log(Array.prototype.at.call(arrayLike, 2)); // undefined
- {{jsxref("Array")}}
- {{jsxref("Array.prototype.findIndex()")}}
- {{jsxref("Array.prototype.indexOf()")}}
- {{jsxref("Array.prototype.with()")}}
- {{jsxref("TypedArray.prototype.at()")}}
- {{jsxref("String.prototype.at()")}}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ A new array with the element at `index` replaced with `value`.

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.

By combining `with()` with {{jsxref("Arra/at", "at()")}}, you can both write and read (respectively) an array using negative indices.

The `with()` method never produces a [sparse array](/en-US/docs/Web/JavaScript/Guide/Indexed_collections#sparse_arrays). If the source array is sparse, the empty slots will be replaced with `undefined` in the new array.

The `with()` method is [generic](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#generic_array_methods). It only expects the `this` value to have a `length` property and integer-keyed properties.
Expand Down Expand Up @@ -100,4 +102,5 @@ console.log(Array.prototype.with.call(arrayLike, 0, 1));
- {{jsxref("Array.prototype.toReversed()")}}
- {{jsxref("Array.prototype.toSorted()")}}
- {{jsxref("Array.prototype.toSpliced()")}}
- {{jsxref("Array.prototype.at()")}}
- {{jsxref("TypedArray.prototype.with()")}}
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@ console.log(atWay); // 11
- {{jsxref("TypedArray")}}
- {{jsxref("TypedArray.prototype.findIndex()")}}
- {{jsxref("TypedArray.prototype.indexOf()")}}
- {{jsxref("TypedArray.prototype.with()")}}
- {{jsxref("Array.prototype.at()")}}
- {{jsxref("String.prototype.at()")}}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ console.log(arr); // Uint8Array [1, 2, 3, 4, 5]
- [JavaScript typed arrays](/en-US/docs/Web/JavaScript/Guide/Typed_arrays) guide
- {{jsxref("TypedArray.prototype.toReversed()")}}
- {{jsxref("TypedArray.prototype.toSorted()")}}
- {{jsxref("TypedArray.prototype.at()")}}
- {{jsxref("Array.prototype.with()")}}

0 comments on commit a815a95

Please sign in to comment.