# New Array Methods

## Static methods

```
Array.of
Array.from
```

## Array.of

```javascript
var arr = Array(4); // arr.length = 4
var arr1 = Array.of(4); // arr1.length = 1 

// create array from the passed arguments
console.log(Array.of(2,3,4,5)); // [2,3,4,5]
```

## Array.from

want to use Array methods on `arguments`

```javascript
// ES5
function greetMe(){
    console.log(arguments.join('--')); // TypeError: arguments.join is not a function
}

console.log(greetMe('hemant', "hello"));
```

```javascript
// ES6
function greetMe(){
    console.log(Array.from(arguments).join('--')); // hemant--hello
}

console.log(greetMe('hemant', "hello"));
```

### Array.from use case 1

```javascript
var paragraphs = document.querySelector('p');

paragraphs.map() // Error

var paragraphsArray = Array.from(paragraphs);
// now you can use map on it

// Array.from also have map as 2nd argument
Array.from(paragraphs, person => person.textContent);
```

### Array.from use case 2

```
// Loop over arguments
function sum(){
    return Array.from(arguments).reduce((prev, next) => prev+next, 0);
}

console.log(sum(1,2,3,4,4,456,6));
```

## Array instance methods

```javascript
[].find()
[].findIndex()
[].copyWithin()
```

## \[].find

```javascript
var arr = [
    { name: "hemant", age: 30},
    { name: "Vinay", age: 20},
    { name: "Varun", age: 88}
];

var myName = arr.find(function(item){
    return item.name === 'Varun';
});

console.log(myName); // { name: "Varun", age: 88}
```

## \[].findIndex()

```javascript
var arr = [
    { name: "hemant", age: 30},
    { name: "Vinay", age: 20},
    { name: "Varun", age: 88}
];

    return item.name === 'Varun';
});
var indexOfName = arr.findIndex(function(item){

console.log(indexOfName); // 2
```

## \[].copyWithin()

```javascript
var arr = [
    { name: "hemant", age: 30},
    { name: "Vinay", age: 20},
    { name: "Varun", age: 88}
];

console.log(arr.copyWithin(2, 0));
// output
[
    { name: "hemant", age: 30},
    { name: "Vinay", age: 20},
    { name: "hemant", age: 30}
]
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hemantajax-2.gitbook.io/es6-in-depth/new-array-methods.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
