Arrays : JavaScript - Coder's Gyan




 Array and loops :-


const languages = ['javaScript','Python','C++']
Lenght
console.log(languages.length);

adding arrays
languages.push('Dart')
languages.unshift('Java')

removing arrays
languages.pop()
languages.shift()

console.log(languages);

LOOPS :-

const actors = [
    {
          name : "Actor 1",
          payment : 100
    },
    {
          name : "Actor 2",
          payment : 200
    },
    {
          name : "Actor 3",
          payment : 150
    },
];

For Loop

for (let i = 0i < actors.lengthi++){
    actors[i].payment = actors[i].payment - 10;
}
console.log(actors);

ForEach Loop 

actors.forEach((actor)=> {
    actor.name = "Name changed"
    actor.payment = actor.payment - 50;
    console.log(actor);
})

ForOff Loops :-

for (let actor of actors){
    actor.payment = actor.payment - 10;
    console.log(actor);
}



Filter

const student = [
      {
            name : "Student 1",
            marks : 45
      },
      {
            name : "Student 2",
            marks : 67
      },
      {
            name : "Student 3",
            marks : 30
      },
]

let failed = student.filter((student=> {
      if(student.marks < 45){
            return true;
      }else
      {
      return false;
      }
})

console.log(failed);

MAP

const users = [
      {
            fName : "John",
            lName : "Doe"
      },
      {
            fName : "Jane",
            lName : "Doe"
      }
];

const finalUsers = users.map((user)=>{
      return {
            fullname : `${user.fName} ${user.lName}`
      };
});

console.log(finalUsers);

Reduce 

let movies = [
      {
            name : "Interstellar",
            budget : 100
      },
      {
            name : "Social",
            budget : 150
      },
      {
            name : "Matrix",
            budget : 300
      },
]

using forEach for example
let total = 0;

movies.forEach((movie=>{
      total = total + movie.budget
})

using reduce 

const total = movies.reduce((acc,movie)=>{
      acc = accmovie.budget;
      return acc;
},0)

console.log(total);


IndexOf
it return index or -1 

const admins = [2,1,5];

const user = {
      name : 'xyz',
      id : 5,
}

const isAdmin = admins.indexOf(user.id) > -1;
console.log(isAdmin);

Includes 
Better than IndexOf

const admins = [2,1,5];

const user = {
      name : 'xyz',
      id : 5,
}

console.log(admins.includes(user.id));

Find 
It return boolean value 
if it dont find anything then it return undefined

const users = [
      {
            name : "xyz",
            id : 1
      },
      {
            name : "abc",
            id : 2
      },
      {
            name : "pqr",
            id : 3
      },
];

const myUser = users.find((user=> {
      if(user.id === 2){
            return true
      }else{
            return false
      }

})

console.log(myUser);


Sort

const names = ['Ram','Shyam','Gita','Babita','Sundar'];

const sortNames = names.sort();
console.log(sortNames);

Splice 
 it delete by index no.

const names = ['Priya','Riya','Anshu','sumit','geeta'];

let deleteName = names.splice(2,1)
console.log(names);