Posts

Showing posts from 2020

How to remove a specific item from an array

How to remove a specific item 9 from an array arr? There are different methods which we can use to remove elements from an arrays const arr = [1, 2, 7, 9, 19]; const index = arr.indexOf(9); if (index > -1) { arr.splice(index, 1); } If we want new array without specific value const result = arr.filter((item) =>{   return item !== 9; });

Es6 how to get matching objects array

Requirement is in array find objects which value is 1 const arr = [ {id:1, value:1}, {id:2, value:2}, {id:3, value:1}, {id:4, value:1} ]; In result we are expecting  //  [{id:1, value:1}, {id:4, value:1} ] const result = arr.filter((item)  => { return item.value === 1; });

How to remove duplicate values from an array

Set object  can be used to remove duplicate values from an array. const arr = [1,4,7,8,1,9,4]; const result = [...new Set(arr)]; Or const result = are.reduce((x, y) => x.includes(y) ? x : [...x, y], []); Or const result = arr.filter((item, index) => { return arr.indexOf(item) === index; });

How to remove undefined values from an array in es6

Image
 Let assume we have an array  const arr = [4,  1, undefined, 2, undefined, 9]; And in result we are expecting  [4,  1,  2, 9] To solve this problem  Const result = arr.filter((item) => {        return item  !== undefined;  }); Console.log(result);

Website design examples

Image