Javascript


React JS 

1 Series learn ReactJs

https://youtube.com/playlist?list=PLyZUcCQtUmW3fuNX7IkaadGPwl2rnqMbZ


2 Action On JavaScript


 

3 Get Properties Child By relattive variable 

 


 

4 Called function in javascript 

 

1 use () in tag html 

 

const callFunction = (row , event) {
 // ... event.target.value
}
<button onClick={ (event) => { callFunction(row , event) } > clickRow </button>

2 use function

 const callFunction = (row) => (event) {
 // ... event.target.value
}
<button onClick={callFunction(row)> clickRow </button>

 

5 Copy Object

 

5.1 if datatype is primitive use =

    let a = 10;
    let b= a;

5.2 if is the object normal not contain object child use 

Use the spread (...) syntax or Use the Object.assign() method Shallow copy first level.

const person = {
    firstName: 'John',
    lastName: 'Doe'
};

// using spread ...
let p1 = {
    ...person
};


// using  Object.assign() method
let p2 = Object.assign({}, person);

5.3 // using JSON for deep level

let person = {
    firstName: 'John',
    lastName: 'Doe',
    address: {
        street: 'North 1st street',
        city: 'San Jose',
        state: 'CA',
        country: 'USA'
    }
};
let p3 = JSON.parse(JSON.stringify(person));
reference: https://www.javascripttutorial.net/object/3-ways-to-copy-objects-in-javascript/

 

6 Copy array in Js

 

6.1 use spread [...] only boolean, number, and string. first level.
ex: [1,4,true,'string'] 
 
6.2 Literal structures [[], {}];
use JSON.parse(JSON.stringify(myArray))
ex: const type2 = [[], {}];
 
6.3 Array of prototype-objects (function)
ex: const type3 = [function () {}, function () {}];
Use cloneDeep of Lo-dash
 

Javascript deep copy techniques by element types

 
  

7 Compare Object 

 

7.1 If Object is primitive 
 
let a = 10 
let b = 10
use a === b // true
 
7.2 Object, new String, new Number, new Boolean.
 
Use JSON.stringify() good for String, Number, Boolean.
 
let a = new Number(4);
let a2 = a ;
let a3 = new Number(4);

a === a2 // true
a === a3 //false
JSON.stringify(a) === JSON.stringify(a3) //true
 
Use JSON.stringify() with object must order must same.
 
 let a = { b: 11 , c:12}
let b = { b: 11 , c:12}
let c = { c:12 , b: 11} // mix updata

JSON.stringify(a) === JSON.stringify(b) //true
JSON.stringify(a) === JSON.stringify(c) //false
 
if you must compare the order mix up use must code 
 
 function deepEqual(x, y) {
  const ok = Object.keys, tx = typeof x, ty = typeof y;
  return x && y && tx === 'object' && tx === ty ? (
    ok(x).length === ok(y).length &&
      ok(x).every(key => deepEqual(x[key], y[key]))
  ) : (x === y);
}
 
 
7.3 Use isEqual library Lodash 
 
 

8 Compare Array

8.1 Use isEqual library Lodash

notice: must same order if not return false.

_.isEqual([1,2,3], [2,1,3]) => false

_.isEqual([1,2,3].sort(), [2,1,3].sort()) => true

instead of isEqual can use difference(). The difference() is not distinguish order.

_.difference([2, 1, 7], [2, 3]); // [1,7]

_.difference([3,2,1], [2, 3,1]); // []


9 Check Object null and undefied and empty (work for object)

https://sharecodefull.blogspot.com/2021/07/check-status-object.html 

 

10 ES 6 some new features

 https://sharecodefull.blogspot.com/2021/09/es-6-some-feature.html

 

 

 

 


0 comments :

Post a Comment

Cancel Reply

About Me

My photo
Tân An, Long An, Vietnam
Hello everyone, I love programming, I love making friends with people all over the world.