How to check Object length in Javascript

–JS

var myObj = {
	name: 'Pradeep',
	age: 28,
	state: 'delhi',
	phone: '12121212'
}
Object.keys(myObj)
(4) ["name", "age", "state", "phone"]
Object.keys(myObj).length
4

Use a new property in Array

We already saw many times array has build in method or property. Like array.sort(), array.slice().

But we want to use array each value double.

Also we can do it with map

arr = [1,2,3,4];
arr.map(function(item){
   return item * 2;
})


But there we can see we need to write code also for map.

But there I want only array.double()

After that each value should be double.

Array.prototype.double = function(){
  var i;
  for (i = 0; i < this.length; i++) {
    this[i] = this[i] * 2;
  }
}

arr = [1,2,3,4];

arr.double()

arr

[vc_single_image image=”978″ img_size=”full”]

connect mongo db atlas to api

add user to clusters mongo db atlas

Basic setup of webpack

Create a folder for webpack with CMD

mkdir webpack-demo && cd webpack-demo

Create package.json

npm init -y

Install webpack and webpack-cli

npm install webpack webpack-cli --save-dev

[vc_single_image image=”957″ img_size=”full”]

Create index.html and src/index.js

[vc_single_image image=”959″ img_size=”full”]

index.html

<!doctype html>
<html>
<head>
<title>Getting Started</title>
<scriptsrc="https://unpkg.com/lodash@4.16.6"></script>
</head>
<body>
<scriptsrc="./src/index.js"></script>
</body>
</html>

index.js

function component() {
    let element = document.createElement('div');

    // Lodash, currently included via a script, is required for this line to work
    element.innerHTML = _.join(['Hello', 'webpack'], ' ');

    return element;
  }

  document.body.appendChild(component());

remove index.js entry from package.json and add private: true.

[vc_single_image image=”962″ img_size=”full”]

Add IP address in Clusters in mongo db atlas

Create new clusters in project in mongo db atlas

Create new project in mongo db atlas

SetInterval in for loop in Javascript

Normally we used interval with normal way.

var i  = 0;
var id = setInterval(function(){
    if(i == 10){
        clearInterval(id)
    } else {
        i++;
        console.log(i);
    }
}, 1000)

[vc_text_separator title=”Now”]

We need to use this with for loop but there we will use setTimeout instead of setInterval.

for(var i=0; i<10; i++){
    (function(i){
        setTimeout(function(){
            console.log(i)
        },1000*i);
    })(i)
}

check typeof in a function

We already know the typeof [] is Object. But we want it should be return Array. For that we will create a function.

function Solution(n){
  if(Array.isArray(n)){
    return 'Array';
  } else {
    return typeof n;
  }
}