Javascript相關

前言

記錄有關一些javascript的用法

UTC的用法

目前是使用momentjs這個程式庫來轉成要的UTC

1
2
3
4
5
6
//將日期塞入,出來就是utc的時間格
moment.utc(formmodel.value.date);
//以數字來使用
moment.utc(formmodel.value.date).valueOf();
//不要毫秒
Math.round(moment.utc(formmodel.value.date).valueOf() / 1000);

Buffer的用法

建立array

這是其中的一個

1
2
3
4
var array1 = Buffer.from("123");
console.log(array1);//<Buffer 31 32 33>
var array2 = Buffer.from("abc");
console.log(array2);//<Buffer 61 62 63>

合併

1
2
3
4
5
6
7
8
9
var array1 = Buffer.from("123");
console.log(array1);//<Buffer 31 32 33>
var array2 = Buffer.from("abc");
console.log(array2);//<Buffer 61 62 63>
var array3 = Buffer.from("ABC");
console.log(array3);//<Buffer 41 42 43>
var totallength = array1.length + array2.length + array3.length;
var buf = Buffer.concat([array1, array2, array3], totallength);
console.log(buf);//<Buffer 31 32 33 61 62 63 41 42 43>

以下是參考的碼

1
2
3
4
5
6
7
8
9
10
var videobuff = Buffer.from(chunk);
var audiobuff = Buffer.from(soundchunk);
var Duration = Buffer.alloc(2);//new Buffer(2);
Duration.writeUInt16LE(1000, 0);
var auddiolen = Buffer.alloc(4);//new Buffer(4);
auddiolen.writeUInt32LE(audiobuff.length, 0);

var totalLength = Duration.length + auddiolen.length + videobuff.length + audiobuff.length;
var buf = []
var buf = Buffer.concat([Duration, auddiolen, audiobuff, videobuff], totalLength);

參考資料

Node.js:Buffer淺談

Buffer 模組處理

日期轉換的用法

將時間的字串,先取出來,再利用dataToYMD轉成相要的顯示字串
new Date().toISOString(); // e.g. “2016-11-21T08:00:00.000Z”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function GetCurDate()
{
var strDate = $("#dummyDate").val().substring(0, 10);
//console.log(strDate);
var d = new Date(Date.parse(strDate));
var szformatDate = dateToYMD(d);
return szformatDate;
}
function dateToYMD(date) {
var d = date.getDate();
var m = date.getMonth() + 1;
var y = date.getFullYear();
return '' + y + '/' + (m <= 9 ? '0' + m : m) + '/' + (d <= 9 ? '0' + d : d);
}

JavaScript Array陣列的操作方法

目前記錄是我有用到的方法

forEach的用法

有index

1
2
3
4
var array1 = ['a', 'b', 'c'];
array1.forEach(function(item,idx) {
console.log(item);
});

無index

1
2
3
4
var array1 = ['a', 'b', 'c'];
array1.forEach(function(item) {
console.log(item);
});

every()和some()

  • every()是對數組中每一項運行給定函數,如果該函數對每一項返回true,則返回true。
  • some()是對數組中每一項運行給定函數,如果該函數對任一項返回true,則返回true。
1
2
3
4
5
6
7
8
9
10
var arr = [1, 2, 3, 4, 5, 6];
console.log(arr.some(function (item, index, array) {
console.log(`item=${item},index=${index},array=${array}`);
return item > 3;
}));

console.log(arr.every(function (item, index, array) {
console.log(`item=${item},index=${index},array=${array}`);
return item > 3;
}));

運行結果

1
2
3
4
5
6
7
8
item=1,index=0,array=1,2,3,4,5,6 
item=2,index=1,array=1,2,3,4,5,6 
item=3,index=2,array=1,2,3,4,5,6 
item=4,index=3,array=1,2,3,4,5,6 
true
#-------------------------------- 
item=1,index=0,array=1,2,3,4,5,6 
false

some一直在找符合條件的值,一旦找到,則不會繼續迭代下去。

every從迭代開始,一旦有一個不符合條件,則不會繼續迭下去。

Promise的相關資訊

理解 Promise 狀態及使用方式

從Promise開始的JavaScript異步生活
Multi File Uploader built with Native JS and Promises

使用 Blob 和 File 相關 Web API 即時呈現上傳圖片檔案