Posts

Showing posts from October, 2020

spread operator use in reactjs

We can use spread operator to add two array const result = [...arr1, ...arr2]; We can copy/clone one array/object to other variable const arr3 = [...arr1]; const obj1 = {...obj1}; Extend array value const arr4 = [ 1, 3 , ...arr1]; function myFunction(...theArgs) {  //   } in react we can pass all props using sprad property &lgt

One button click navigate to route path in reactjs

On click of element if we want to navigate route path- import { useHistory } from "react-router-dom";  < button onclick="{handleClick}" type="button">   home </button> const history = useHistory();  function handleClick() {           history.push("/home");  }

How to do sorting array object es6

 To sort objects by its vale  const arr = [   {name:"abc", price: 5},     {name:"pqr", price: 3},    {name:"mno", price: 2}  ]; const result = arr.sort((a,b) => a.price-b.price );

How to merge two array and remove duplicate es6

To merge two array we can use spread operator and remove duplicate const arr1 = ["abc","efg"];  const arr2 = ["efg", "pqr"]; const arr3 = [...array1, ...array2]; const result = [...new Set([arr3)];

How to check any property available in object es6

 To check whether property available in object const result = myObj.hasOwnProperty('id');

How to count length of object keys es6

To check the length of object (obj) keys const result = Object.keys(obj).length

How to filter one array from other array es6

const filtered = arr1.filter( (e) =>  !arr2.includes(e.key));

Find matched object in es6

How to get matched id object or other value?  const arr = [{'id': 1, ‘name’: ‘abc’'},{'id': 5,  ‘name':'xyz'}]; const result = arr.find((v) => v.id === 5); if we want an array of matching elements: const result = arr.filter((v) => v.id === 5); The find() method returns the first value in the array, if an element in the array satisfies the provided testing function. Otherwise return undefined.

Elements position in JavaScript

  getBoundingClientRect() is a javascript function that returns the position of the element relative to viewport of window. const position = selector.getBoundingClientRect(); const result = position.top + window.scrollY;

Break loop once condition match JavaScript

const arr = [1,2,3];  Iterations loops: for  every()  some() We can use: for(let item of arr){   console.log(item);  if(item == 2){      break;   }  } Or for (var i = 0; i < arr.length; i++) {   console.log(arr[i]);   if (arr[i] === 2) {        break;   }  } Or arr.every((elm) => {   console.log(elem);   if (elm === 2) {        return true;  }  return false; }); Or  arr.every((elm) => {   console.log(elem);   if (elm === 2) {        return false;  }  return true;  });

How to check variable is an array es6

const arr = [1,5,7]; const result = arr.constructor === Array;  const result = Array.isArray(arr);  Or in Lodash:  const result =  _.isArray(arr); note: typeof []; // returns 'object' and arr.length check will fail if variable is a string.

How to remove last character from string value?

How to remove last character from string value? const val = "11.125";  const result = val.slice(0, -1); Or const result = val.substring(0, val.length - 1); Only if we need numeric value and ristrict decimal value two digit for above case  let numb = parseFloat(val); numb = numb.toFixed(2);

Window height detect es6

const result  = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; Or const result = window.screen.availHeight; Or const result = window.innerHeight; in jquery :  $(window).height(); // Full height of the HTML page, including content   $(document).height(); // document's viewport height

How to iterate object key/values es6

How to iterate object key/values? const obj = { a: "foo", b: "bar" }; const result = Object.keys(obj).forEach((key,i) => {    // ..  });

How to empty an array?

How to empty an array? const arr = [1,4,6]; arr =  [];  Or arr.length = 0;  Or arr.splice(0, arr.length);  Or while (arr.length > 0) { arr.pop(); }

Es6 how to check if value is not null or undefined?

How to check if value id null or undefined.  simple way is just check if has value! Like  if(value) { // ... }  will return true if value is not:   null, undefined, NaN, empty string (""),  0, false It’s fulfill our requirements but also check additional conditions  Like we want to check id is null or undefined but in response we got id = 0 In this condition it will fail because we just need to check null or undefined  if(value !== undefined || value !== null){ // ... }

How to define optional parameters to a function?

How to define optional parameters to a function? functio myFunction(row = 1, data = []){ // ... function body }

How to format a date object?

How to format a date object? const td = new Date(); const result = td.getDate() + "/" + (td.getMonth()) + "/" + td.getFullYear(); Or const result = new Intl.DateTimeFormat('en-US').format(td); Or if we need complex or quick solutions of date format then MomentJS is a good option. import moment js const result = moment().format('YYYY-MM-DD HH:m:s');

Check the existence of a key in JavaScript Object

How to check the existence of a key in JavaScript Object const myObj = { a: 1, b: 2}; We need to check whether I’d exist in object or not! const result = myObj.hasOwnProperty('id'); // false Or const result = 'id' in myObj; // false Or const result = myObj.id !== undefined; // false

How to detect object length in es6

const myObj = {"name" : "abc", "city" : "xyz"};   const result = Object.keys(myObj).length;

How add item to an array at a specific index es6

const arr = [11, 33 ]; We want to add 22 after 11 const result = arr.splice(1, 0, 22);

How to check key exists in object es6

How to check if a particular key exists in a JavaScript object? const result = myArr["rate"] === undefined; Or const result = myArr.hasOwnProperty("rate");

Es6 how to check object is empty

 How to check object is empty?  const myObj = {}; const result = Object.keys(myObj).length === 0); and

How to Check If a Value Exists in an Array using es6

How to Check If a Value Exists in an Array const arr = ["sam","david","tom"];  In above we need to check whether david include s in are or not const result = arr.contains("david");

How to remove property from object in es6

const myObject = { "name": "my name", "city": "my city", "age": 18 }; we want to remove age property from above object delete myObject.age; delete myObject['age'];

How to check string contains in another string in es6

 we can use contains(), indexOf() method to check if one String contains another String in JavaScript/es6 const string = "JavaScript"; const substring = "Java"; const result = string.includes(substring)); or   const results = string.indexOf(substr) !== -1;

How to remove undefined values from an array - es6

  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;  });

How to remove duplicate values from an array - es6

  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 get matching objects array - Es6

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 a specific item from an array in es6

  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; });

Advertiserment website design

Image
 Below one of the sample design you can refer for advertisement agencies.

Website design idea

Image
 

Website design idea

Image
 

Logo design idea - contractspots

Image
  Sample logos which you can refer first logo created as logo symbol of contract and in second logo spot highlighted in o character. 

Logo design idea

Image
 

How to Remove Duplicates from Array in Javascript ES6

How to Remove Array Duplicates in ES6 Below is the sample array we have, class7 comes two times. var arr = ["class5","class7","class2","class7"]; we want to remove the duplicates value var result = [...new Set(arr)]; // result = ["class5","classs7","class2"] Now lets assume we have array object and we need to remove objects which has duplicate id var arr = [ {id: 1, name: "test"}, {id: 2, name: "test2"}, {id: 1, name: "test3"}, {id: 3, name: "test4"} ] var result = [...new Set(arr.map(({id}) => id))].map((val) => arr.find(({id}) => id === val));

How to Convert String with comma to Array in Javascript - es6

Convert string with commas to array using the split() Method lets assume we have a variable string where values are comma separated. we will split this by the comma, and then store it in an array. var numStr = "1, 5, 8"; var numArr = numStr.split(","); // result numArr = ["1", "5","8"] this split numStr from ',' and convert in an array if you want numbers in array: [1, 5, 8] var numArr = str.split(',').map(Number);