Thứ Năm, 1 tháng 4, 2021

Javascript Scripting

 https://playcode.io/new/


## If Else
if (time < 10) {
greeting = "Good morning";
}
else if (time < 20) {
greeting = "Good day";
}
else {
greeting = "Good evening";
}

## Switch Case
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

## While
while (condition) {
// execute code as long as condition is true
}

## Do .. While
do {
do_work();
} while (condition);

## For
var array = ['C++', 'Java', 'JavaScript', 'NodeJS'];
for(var i = 0; i < array.length; i++){
console.log(array[i]);
}

## For const - of a array
var data = ['C++', 'Java', 'JavaScript', 'NodeJS'];
for (const item of data) {
console.log(item);
}

## For const - of with a object
var data = {'a':1, 'b':2, 'c':3};
for (const [key, value] of Object.entries(data)) {
console.log(key + "-" + value);
}

## For each a list string
const arr = ['cat', 'dog', 'fish'];
arr.forEach(function(element) { console.log(element); });

arr.forEach(element => { console.log(element); });

## For each a list number
const arr = [1, 2, 3, 8, 7];
arr.forEach(element => {
console.log(element);
});

## For each a array and reverse
const arr = [1, 2, 3, 8, 7];

var reversed = arr.slice().reverse();
reversed.forEach(function (item, index) {
console.log(index + "-" + item);
});

## For each with `lodash`/forEach Module
const _ = require('lodash')
// OR const _forEach = require("lodash/forEach")

const processItems = async (orderItems, isOperator = false) => {
let itemIds = Helper.extractToArray(orderItems, 'id')
let items = await ItemModel.getItemListByItemIds(itemIds, undefined, true, isOperator)
_.forEach(items, (item, index) => {
orderItems[index].images = item.images
orderItems[index].slug = item.slug || ""
orderItems[index].shopSlug = item.shop.slug || ""
delete orderItems[index].image
})
return orderItems
}

## Filter list values from a list array object
let array = [
{id: 1, name: 'foo'},
{id: 2, name: 'bar'},
];
let names = array.map( el => el.name );
console.log(names);

## For a array have object item
var arrayB = [1,2,3,{"fruit":"orange", "drink":"beer"}];
for (const item of arrayB) {
console.log(item);
if (typeof item === "object") {
console.log(item.fruit);
}
}

## multi-dimensional arrays
var arr = [['a', 'b'], ['c','d']];
for (const item of arr) {
console.log(item);
}

## For in for get index key
var arr = [['a', 'b'], ['c','d']];
for (var key in arr) {
console.log(key);
}

## Normal Object
var validation_messages = {
"key_1": {
"your_name": "jimmy",
"your_msg": "hello world"
},
"key_2": {
"your_name": "billy",
"your_msg": "foo equals bar"
}
}

for (const [key, value] of Object.entries(validation_messages)) {
console.log(key);
console.log(value);
}

## Process json data in javascript
var objJSON = JSON.parse('{ "name":"Krunal", "age":25, "city":"Rajkot", "degree": "BE"}');
console.log(objJSON);
console.log(objJSON.name);


Không có nhận xét nào:

Đăng nhận xét

Học lập trình web căn bản với PHP

Bài 1: Các kiến thức căn bản Part 1:  https://jimmyvan88.blogspot.com/2012/05/can-ban-lap-trinh-web-voi-php-bai-1-cac.html Part 2:  https://...