moment 和 Array 時間和數組使用及相關注意事項


moment的使用及相關注意事項

一、時間戳是什么?

時間戳(timestamp),一個能表示一份數據在某個特定時間之前已經存在的、 完整的、 可驗證的數據,通常是一個字符序列,唯一地標識某一刻的時間。

var date = new Date('2020-12-01 11:51:11:123');
// 有三種方式獲取
// 精確到毫秒
var time1 = date.getTime();
console.log(time1);//1606794671123
// 精確到毫秒
var time2 = date.valueOf();
console.log(time2);//1606794671123
// 只能精確到秒,毫秒用000替代
var time3 = Date.parse(date);
console.log(time3);//1606794671000

vue里面直接引入使用就可以
momentd的地址http://momentjs.cn/

moment的常用操作

1.首先兩個moment的時間可以直接比較大小 返回true 或者 false

2.計算時間差 this.end.diff(this.start, "hour") 傳入什么 返回的就相差多少

3.按照時間格式進行格式化 例如:moment().format('YYYY-MM-DD HH:mm:ss') //2020-12-01 13:45:21
你也可以自定義的去返回 比如 :moment(value).format('YYYY-MM-DDTHH:mm') //2017-12-14T16:34

4.取當前時間的前一天 moment().subtract(1, 'days')

5.返回當前的時間戳 moment().unix();

6.還有設置時間 啥的

moment().year(Number), moment().month(Number)... 
moment().set(String, Int) 
moment().set(Object)

還有好多 文檔里面都有,總之就是蠻好用的

二、數組的操作

就是隨便寫寫,感覺的整理一下
其實官方的文檔上都有
文檔展示https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map
但還是寫寫好點

非常建議有時間從基礎好好看看,記得看過有個寫ES6的系列文章的,寫的很細,只有那個樣子,才能寫大項目的時候不容易運轉出問題 標簽啥的,內存消耗

下邊是我整理別人的
一、concat()
concat() 方法用於連接兩個或多個數組。該方法不會改變現有的數組,僅會返回被連接數組的一個副本。
var arr1 = [1,2,3];
var arr2 = [4,5];
var arr3 = arr1.concat(arr2);
console.log(arr1); //[1, 2, 3]
console.log(arr3); //[1, 2, 3, 4, 5]

二、join()
join() 方法用於把數組中的所有元素放入一個字符串。元素是通過指定的分隔符進行分隔的,默認使用','號分割,不改變原數組。
var arr = [2,3,4];
console.log(arr.join()); //2,3,4
console.log(arr); //[2, 3, 4]

三、push()
push() 方法可向數組的末尾添加一個或多個元素,並返回新的長度。末尾添加,返回的是長度,會改變原數組。
var a = [2,3,4];
var b = a.push(5);
console.log(a); //[2,3,4,5]
console.log(b); //4
push方法可以一次添加多個元素push(data1,data2....)

四、pop()
pop() 方法用於刪除並返回數組的最后一個元素。返回最后一個元素,會改變原數組。
var arr = [2,3,4];
console.log(arr.pop()); //4
console.log(arr); //[2,3]

五、shift()
shift() 方法用於把數組的第一個元素從其中刪除,並返回第一個元素的值。返回第一個元素,改變原數組。
var arr = [2,3,4];
console.log(arr.shift()); //2
console.log(arr); //[3,4]

六、unshift()
unshift() 方法可向數組的開頭添加一個或更多元素,並返回新的長度。返回新長度,改變原數組。
var arr = [2,3,4,5];
console.log(arr.unshift(3,6)); //6
console.log(arr); //[3, 6, 2, 3, 4, 5]
tip:該方法可以不傳參數,不傳參數就是不增加元素。

七、slice()
返回一個新的數組,包含從 start 到 end (不包括該元素)的 arrayObject 中的元素。返回選定的元素,該方法不會修改原數組。
var arr = [2,3,4,5];
console.log(arr.slice(1,3)); //[3,4]
console.log(arr); //[2,3,4,5]

八、splice()
splice() 方法可刪除從 index 處開始的零個或多個元素,並且用參數列表中聲明的一個或多個值來替換那些被刪除的元素。如果從 arrayObject 中刪除了元素,則返回的是含有被刪除的元素的數組。splice() 方法會直接對數組進行修改。
var a = [5,6,7,8];
console.log(a.splice(1,0,9)); //[]
console.log(a); // [5, 9, 6, 7, 8]
var b = [5,6,7,8];
console.log(b.splice(1,2,3)); //[6, 7]
console.log(b); //[5, 3, 8]

九、substring() 和 substr()
相同點:如果只是寫一個參數,兩者的作用都一樣:都是是截取字符串從當前下標以后直到字符串最后的字符串片段。
substr(startIndex);
substring(startIndex);
var str = '123456789';
console.log(str.substr(2)); // "3456789"
console.log(str.substring(2)) ;// "3456789"
不同點:第二個參數
substr(startIndex,lenth): 第二個參數是截取字符串的長度(從起始點截取某個長度的字符串);
substring(startIndex, endIndex): 第二個參數是截取字符串最終的下標 (截取2個位置之間的字符串,‘含頭不含尾')。
console.log("123456789".substr(2,5)); // "34567"
console.log("123456789".substring(2,5)) ;// "345"

十、sort 排序
按照 Unicode code 位置排序,默認升序
var fruit = ['cherries', 'apples', 'bananas'];
fruit.sort(); // ['apples', 'bananas', 'cherries']
var scores = [1, 10, 21, 2];
scores.sort(); // [1, 10, 2, 21]

十一、reverse()
reverse() 方法用於顛倒數組中元素的順序。返回的是顛倒后的數組,會改變原數組。
var arr = [2,3,4];
console.log(arr.reverse()); //[4, 3, 2]
console.log(arr); //[4, 3, 2]

十二、indexOf 和 lastIndexOf
都接受兩個參數:查找的值、查找起始位置 不存在,返回 -1 ;存在,返回位置。indexOf 是從前往后查找, lastIndexOf 是從后往前查找。
indexOf
var a = [2, 9, 9];
a.indexOf(2); // 0
a.indexOf(7); // -1

if (a.indexOf(7) === -1) {
// element doesn't exist in array
}
lastIndexOf

var numbers = [2, 5, 9, 2];
numbers.lastIndexOf(2); // 3
numbers.lastIndexOf(7); // -1
numbers.lastIndexOf(2, 3); // 3
numbers.lastIndexOf(2, 2); // 0
numbers.lastIndexOf(2, -2); // 0
numbers.lastIndexOf(2, -1); // 3

十三、every
對數組的每一項都運行給定的函數,每一項都返回 ture,則返回 true
function isBigEnough(element, index, array) {
return element < 10;
}
[2, 5, 8, 3, 4].every(isBigEnough); // true

十四、some
對數組的每一項都運行給定的函數,任意一項都返回 ture,則返回 true
function compare(element, index, array) {
return element > 10;
}
[2, 5, 8, 1, 4].some(compare); // false
[12, 5, 8, 1, 4].some(compare); // true

十五、filter
對數組的每一項都運行給定的函數,返回 結果為 ture 的項組成的數組
var words = ["spray", "limit", "elite", "exuberant", "destruction", "present", "happy"];
var longWords = words.filter(function(word){
return word.length > 6;
});
// Filtered array longWords is ["exuberant", "destruction", "present"]
還可以做篩選
var a = [ {name:123,id:1},{name:456,id:2},{name:789,id:3} ]
a.filter( item => { return item.id = 1} )
// [ {name:123,id:1} ]

十六、map
對數組的每一項都運行給定的函數,返回每次函數調用的結果組成一個新數組
var numbers = [1, 5, 10, 15];
var doubles = numbers.map(function(x) {
return x * 2;
});
// doubles is now [2, 10, 20, 30]
// numbers is still [1, 5, 10, 15]

十七、forEach 數組遍歷
const items = ['item1', 'item2', 'item3'];
const copy = [];
items.forEach(function(item){
copy.push(item)
});

ES6新增新操作數組的方法

1、find():
傳入一個回調函數,找到數組中符合當前搜索規則的第一個元素,返回它,並且終止搜索。
const arr = [1, "2", 3, 3, "2"]
console.log(arr.find(n => typeof n === "number")) // 1
2、findIndex():
傳入一個回調函數,找到數組中符合當前搜索規則的第一個元素,返回它的下標,終止搜索。
const arr = [1, "2", 3, 3, "2"]
console.log(arr.findIndex(n => typeof n === "number")) // 0
3、fill():
用新元素替換掉數組內的元素,可以指定替換下標范圍。
arr.fill(value, start, end)
4、copyWithin():
選擇數組的某個下標,從該位置開始復制數組元素,默認從0開始復制。也可以指定要復制的元素范圍。
arr.copyWithin(target, start, end)
const arr = [1, 2, 3, 4, 5]
console.log(arr.copyWithin(3))
// [1,2,3,1,2] 從下標為3的元素開始,復制數組,所以4, 5被替換成1, 2
const arr1 = [1, 2, 3, 4, 5]
console.log(arr1.copyWithin(3, 1))
// [1,2,3,2,3] 從下標為3的元素開始,復制數組,指定復制的第一個元素下標為1,所以4, 5被替換成2, 3
const arr2 = [1, 2, 3, 4, 5]
console.log(arr2.copyWithin(3, 1, 2))
// [1,2,3,2,5] 從下標為3的元素開始,復制數組,指定復制的第一個元素下標為1,結束位置為2,所以4被替換成2
5、from
將類似數組的對象(array-like object)和可遍歷(iterable)的對象轉為真正的數組
const bar = ["a", "b", "c"];
Array.from(bar);
// ["a", "b", "c"]

Array.from('foo');
// ["f", "o", "o"]
6、of
用於將一組值,轉換為數組。這個方法的主要目的,是彌補數組構造函數 Array() 的不足。因為參數個數的不同,會導致 Array() 的行為有差異。
Array() // []
Array(3) // [, , ,]
Array(3, 11, 8) // [3, 11, 8]
Array.of(7); // [7]
Array.of(1, 2, 3); // [1, 2, 3]
Array(7); // [ , , , , , , ]
Array(1, 2, 3); // [1, 2, 3]
7、entries() 返回迭代器:返回鍵值對
//數組
const arr = ['a', 'b', 'c'];
for(let v of arr.entries()) {
console.log(v)
}
// [0, 'a'] [1, 'b'] [2, 'c']

//Set
const arr = new Set(['a', 'b', 'c']);
for(let v of arr.entries()) {
console.log(v)
}
// ['a', 'a'] ['b', 'b'] ['c', 'c']

//Map
const arr = new Map();
arr.set('a', 'a');
arr.set('b', 'b');
for(let v of arr.entries()) {
console.log(v)
}
// ['a', 'a'] ['b', 'b']
8、values() 返回迭代器:返回鍵值對的value

//數組
const arr = ['a', 'b', 'c'];
for(let v of arr.values()) {
console.log(v)
}
//'a' 'b' 'c'

//Set
const arr = new Set(['a', 'b', 'c']);
for(let v of arr.values()) {
console.log(v)
}
// 'a' 'b' 'c'

//Map
const arr = new Map();
arr.set('a', 'a');
arr.set('b', 'b');
for(let v of arr.values()) {
console.log(v)
}
// 'a' 'b'
9、keys() 返回迭代器:返回鍵值對的key
22
//數組
const arr = ['a', 'b', 'c'];
for(let v of arr.keys()) {
console.log(v)
}
// 0 1 2

//Set
const arr = new Set(['a', 'b', 'c']);
for(let v of arr.keys()) {
console.log(v)
}
// 'a' 'b' 'c'

//Map
const arr = new Map();
arr.set('a', 'a');
arr.set('b', 'b');
for(let v of arr.keys()) {
console.log(v)
}
// 'a' 'b'
10、includes
判斷數組中是否存在該元素,參數:查找的值、起始位置,可以替換 ES5 時代的 indexOf 判斷方式。indexOf 判斷元素是否為 NaN,會判斷錯誤。
var a = [1, 2, 3];
a.includes(2); // true
a.includes(4); // false·

玩一玩蠻好的。。。

有時間得整理 那些是新增的語法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM