Skip to content

Commit

Permalink
fix: corrected the filename and did some refacoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh672003 committed Nov 13, 2023
1 parent 3e2bf1d commit 2862187
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions JavaSript/arrayMethods.md → JavaSript/array-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ Syntax: `arr.splice(index[, deleteCount, elem1, ..., elemN])`

The mentioned elements are inserted at the index.

```javascript
```js
let arr = ["I", "study", "JavaScript", "right", "now"];

// remove 3 first elements and replace them with another
arr.splice(0, 3, "Let's", "dance");
alert(arr); // now ["Let's", "dance", "right", "now"]
```

```javascript
```js
let arr = ["I", "study", "JavaScript", "right", "now"];

// remove 2 first elements
Expand All @@ -52,7 +52,7 @@ Syntax: `arr.slice([start], [end])`

Returns a new array containing all items from index `start` to `end` (not including `end`).

```javascript
```js
let arr = ["t", "e", "s", "t"];

alert(arr.slice(1, 3)); // e,s (copy from 1 to 3)
Expand All @@ -65,7 +65,7 @@ Syntax: `arr.concat(arg1, arg2...)`

Creates a new array that includes values from other arrays and additional items.

```javascript
```js
let arr = [1, 2];

// create an array from: arr and [3,4]
Expand All @@ -82,13 +82,13 @@ alert(arr.concat([3, 4], 5, 6)); // 1,2,3,4,5,6

Syntax:

```javascript
```js
arr.forEach(function(item, index, array) {
... do something with item
});
```

```javascript
```js
["Bilbo", "Gandalf", "Nazgul"].forEach((item, index, array) => {
alert(`${item} is at index ${index} in ${array}`);
});
Expand All @@ -100,7 +100,7 @@ Syntax: `arr.indexOf(item, from)` and `arr.lastIndexOf(item, from)`

Returns the index of the item if found, else -1.

```javascript
```js
let fruits = ["Apple", "Orange", "Apple"];

alert(fruits.indexOf("Apple")); // 0 (first Apple)
Expand All @@ -113,7 +113,7 @@ Syntax: `arr.includes(item, from)`

Returns true if the array has the item, else false.

```javascript
```js
let fruits = ["Apple", "Orange", "Apple"];
alert(fruits.includes("Apple")); // true
```
Expand All @@ -122,21 +122,21 @@ alert(fruits.includes("Apple")); // true

Syntax:

```javascript
```js
arr.find(function (item, index, array) {
// if true is returned, item is returned and iteration is stopped
// for falsy scenario returns undefined
});
```

```javascript
```js
arr.findIndex(function (item, index, array) {
// if true is returned, item is returned and iteration is stopped
// for falsy scenario returns undefined
});
```

```javascript
```js
let users = [
{ id: 1, name: "John" },
{ id: 2, name: "Pete" },
Expand All @@ -155,7 +155,7 @@ alert(users.findLastIndex((user) => user.name == "John")); // 3

Syntax:

```javascript
```js
let results = arr.filter(function (item, index, array) {
// if true item is pushed to results and the iteration continues
// returns empty array if nothing found
Expand All @@ -164,7 +164,7 @@ let results = arr.filter(function (item, index, array) {

Example:

```javascript
```js
let users = [
{ id: 1, name: "John" },
{ id: 2, name: "Pete" },
Expand All @@ -180,15 +180,15 @@ alert(someUsers.length); // 2

Syntax:

```javascript
```js
let result = arr.map(function (item, index, array) {
// returns the new value instead of item
});
```

Example:

```javascript
```js
let lengths = ["Bilbo", "Gandalf", "Nazgul"].map((item) => item.length);
alert(lengths); // 5,7,6
```
Expand All @@ -201,7 +201,7 @@ By default, it sorts the values as strings + modifies the older string.

Use `localeCompare` for Strings:

```javascript
```js
let countries = ["Österreich", "Andorra", "Vietnam"];

alert(countries.sort((a, b) => (a > b ? 1 : -1))); // Andorra, Vietnam, Österreich (wrong)
Expand All @@ -210,7 +210,7 @@ alert(countries.sort((a, b) => a.localeCompare(b))); // Andorra,Österreich,Viet

Use `compareFunction` for numbers:

```javascript
```js
function compareNumeric(a, b) {
if (a > b) return 1;
if (a == b) return 0;
Expand All @@ -230,7 +230,7 @@ Reverses the order

of the elements in the array.

```javascript
```js
let arr = [1, 2, 3, 4, 5];
arr.reverse();
```
Expand All @@ -241,7 +241,7 @@ Syntax: `str.split(delim)`

Splits the string into an array by the given delimiter delim.

```javascript
```js
let names = "Bilbo, Gandalf, Nazgul";
let arr = names.split(", ");
for (let name of arr) {
Expand All @@ -255,7 +255,7 @@ Syntax: `arr.join(glue)`

Joins the array into a string using glue.

```javascript
```js
let arr = ["Bilbo", "Gandalf", "Nazgul"];
let str = arr.join(";"); // glue the array into a string using ;
alert(str); // Bilbo;Gandalf;Nazgul
Expand All @@ -265,7 +265,7 @@ alert(str); // Bilbo;Gandalf;Nazgul

Syntax:

```javascript
```js
arr.reduce(
function (accumulator, item, index, array) {
// ...
Expand All @@ -274,7 +274,7 @@ arr.reduce(
);
```

```javascript
```js
let arr = [1, 2, 3, 4, 5];

// removed initial value from reduce (no 0)
Expand All @@ -286,7 +286,7 @@ alert(result); // 15

Syntax: `arr.some(function(item, index, array) { // returns true if the function returns true for any array element })`

```javascript
```js
const array = [1, 2, 3, 4, 5];
const even = (element) => element % 2 === 0; // Checks whether an element is even
console.log(array.some(even)); // true
Expand All @@ -296,7 +296,7 @@ console.log(array.some(even)); // true

Syntax: `arr.every(function(item, index, array) { // returns true if the function returns true for all array elements })`

```javascript
```js
const isBelowThreshold = (currentValue) => currentValue < 40;
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold)); // true
Expand All @@ -308,7 +308,7 @@ Syntax: `arr.copyWithin(target, start, end)`

Copies its elements to another position in the array, overwriting the existing values.

```javascript
```js
let arr = [1, 2, 3, 4, 5];
arr.copyWithin(0, 3); // copy to 0th index from the 3rd index
alert(arr); // 4,5,3,4,5
Expand All @@ -320,7 +320,7 @@ Syntax: `arr.fill(value, start, end)`

Fills the array with repeating value from index start to end.

```javascript
```js
let arr = [1, 2, 3, 4];

// Fill with 0 from position 2 until position 4
Expand All @@ -337,7 +337,7 @@ Syntax: `arr.flat(depth)`

Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

```javascript
```js
const arr1 = [0, 1, 2, [3, 4]];
console.log(arr1.flat()); // Array [0, 1, 2, 3, 4]

Expand All @@ -351,15 +351,15 @@ console.log(arr2.flat(Infinity)); // Array [0, 1, 2, 3, 4, 5]

Syntax:

```javascript
```js
arr.flatMap(function callback(currentValue[, index[, array]]) {
// return element for new_array
}[, thisArg])
```

First maps each element using a mapping function, then flattens the result into a new array.

```javascript
```js
const arr = [1, 2, 1];
const results = arr.flatMap((num) => (num === 2 ? [2, 2] : 1));
console.log(results); // Array [1, 2, 2, 1]
Expand Down

0 comments on commit 2862187

Please sign in to comment.