GithubHelp home page GithubHelp logo

js-array's Introduction

JS-array

arrays are used to store multiple values in a single variable

declare an array

const arr = [];
//arr is a variable where an empty array been declared
arr[0] = 1;
//declared 1 as a value of index 0
arr[1] = 2;
arr[2] = 3;
arr[3] = 5;
console.log(arr.length); //arr.length gives the length of arr
//output: 4
console.log(arr);
//output:: [1,2,3,5]
arr[10] = 11;
console.log(arr);
//output: [ 1, 2, 3, 5, <6 empty items>, 11 ]
const newArr = ["john", "doe", "steve", "mike"];
//init newArr with value - 'john','doe','steve','mike'
console.log(newArr);
//[ 'john', 'doe', 'steve', 'mike' ]
newArr[4] = "smith";
console.log(newArr);
//[ 'john', 'doe', 'steve', 'mike', 'smith' ]

declare array with constructor pattern

const arrOne = new Array(); //[]
console.log(arrOne, arrOne.length);
//output: [] 0
const arrTwo = new Array(10 /**declaring array length 10 */);
console.log(arrTwo, arrTwo.length);
//output: [ <10 empty items> ] 10

declare array with factory pattern

const arrThree = Array(); //[]
console.log(arrThree, arrThree.length);
//output: [] 0
const arrFour = Array(10 /**declaring array length 10 */);
console.log(arrFour, arrFour.length);
//output: [ <10 empty items> ] 10

Array traverse

const arr = [1, 2, 3, 4, 5, 122, 20, 35, 56, 87, 31, 9, 35];
let sum = 0;
let maxNum = arr[0];
for (let i = 0; i < arr.length; i++) {
  sum = sum + arr[i];
  if (arr[i] > maxNum) {
    maxNum = arr[i];
  }
}
const avg = sum / arr.length;
console.log(`average: ${avg}`);
console.log(`sum is ${sum}`);
console.log(`max num is : ${maxNum}`);

Add element to the end of an array

const arrOne = [1, 2, 3, 4, 5];
const arrTwo = [9, 10];
console.log("arr: ", arrOne);
// [ 1, 2, 3, 4, 5 ]
arrOne.push(6, 7);
console.log("arr: ", arrOne);
// [1, 2, 3, 4, 5, 6, 7]
arrOne.push(arrTwo);
console.log("arr: ", arrOne);
// [ 1, 2, 3, 4, 5, 6, 7, [ 9, 10 ] ]

Add element to the beginning of an array

example-1

const arr= = ["john", "doe", "smith"];
console.log(arrThree);
arr.unshift("Steve");
console.log(arrThree);

example-2

const arrThree = ["john", "doe", "smith"];
console.log(arrThree); //[ 'john', 'doe', 'smith' ]
const arrFour = ["steve", "mike"];
arrThree.unshift(...arrFour);
console.log(arrThree); //[ 'steve', 'mike', 'john', 'doe', 'smith' ]

js-array's People

Contributors

mohiuddinrabby avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.