js-小技巧和小方法
1.字符串首字母大写
var str="xiamgfafsfsfdsf fdsfsfsf"
str = str.substring(0,1).toUpperCase()+str.substring(1);
console.log(str) //Xiamgfafsfsfdsf fdsfsfsf
2.创建一个随机的id
function createRandomId() {
return (Math.random() * 10000000).toString(16).substr(0, 4) + '-' + (new Date()).getTime() + '-' + Math.random().toString().substr(2, 5);
}
3.日期排序
function sortDownDate(a, b) {
return Date.parse(a.dates) - Date.parse(b.dates);
}
function sortUpDate(a, b) {
return Date.parse(b.dates) - Date.parse(a.dates);
}
var arr=[
{
sender: "a",
dates: "2017-08-18 13:23:21",
age: "21"
},
{
sender: "lon",
dates: "2017-04-18 13:23:21",
age: "22"
},
{
sender: "son",
dates: "2017-06-18 13:23:21",
age: "22"
},
{
sender: "on",
dates: "2017-08-18 13:23:21",
age: "22"
},
];
console.log(arr.sort(sortDownDate));//正序
console.log(arr.sort(sortUpDate));//反序
4.日期格式的转换:20190120转为2019-01-20
let str = '20181122';
let result = str.replace(/(\d{4})(\d{2})(\d{2})/g, '$1-$2-$3');
5.姓名的打码(打星)
function formatName(str) {
return new Array(str.length).join('*') + str.substr(-1);
}
6.手机号码,姓名的打码的方法
function RegPhone(str, frontLen, endLen) {
var len = str.length - frontLen - endLen;
var star= '';
for (var i = 0; i < len; i++) {
star+= '*';
}
return str.substring(0, frontLen) + star+ str.substring(str.length - endLen);
}
7.删除数组的某项的方法
(一)、
var arrs = [{
"id": "1",
"name": "小明",
"age": "25"
},
{
"id": "2",
"name": "小华",
"age": "24"
},
{
"id": "3",
"name": "小佳",
"age": "27"
},
{
"id": "4",
"name": "小花",
"age": "22"
}];
let new_data = arrs.filter(function(element, index) {
return element['age'] >= 25;
});
console.log(new_data);//[{id: "1", name: "小刘", age: "25"},{id: "3", name: "小佳", age: "27"}]
(二)、
var data = [{
"id": "1",
"name": "小刘",
"age": "25"
},
{
"id": "2",
"name": "小华",
"age": "24"
},
{
"id": "3",
"name": "小佳",
"age": "30"
}
];
var data1 = []
data.filter(function(e) {
if (e.age > 25) {
data1.push(e)
}
})
console.log(data1) //{id: "3", name: "小佳", age: "30"}
(三)、
var data = [
{ "id": "1", "name": "小刘", "age": "25" },
{ "id": "2", "name": "小华", "age": "24" },
{ "id": "3", "name": "小佳", "age": "27" },
{ "id": "4", "name": "小王", "age": "24" },
{ "id": "5", "name": "小何", "age": "22" },
];
var newData= data.filter(v=>v.age >= 25)
console.log(newData)
(四)、
var data = [{
"id": "1",
"name": "小刘",
"age": "25"
},
{
"id": "2",
"name": "小华",
"age": "24"
},
{
"id": "3",
"name": "小佳",
"age": "27"
},
{
"id": "4",
"name": "小王",
"age": "24"
},
{
"id": "5",
"name": "小何",
"age": "22"
},
{
"id": "6",
"name": "小咪咪",
"age": "35"
}
];
function splices() {
for (let i = 0; i < data.length; i++) {
if (parseInt(data[i].age) < 26) {
data.splice(i, 1);
i--;
}
}
console.log(data);
}
console.log(splices())
(五)、
var data = [{
"id": "1",
"name": "小刘",
"age": "25"
},
{
"id": "2",
"name": "小华",
"age": "24"
},
{
"id": "3",
"name": "小佳",
"age": "27"
},
{
"id": "4",
"name": "小王",
"age": "24"
},
{
"id": "5",
"name": "小何",
"age": "22"
},
];
var newData = [];
data.forEach(function(item, index) {
if (parseInt(item["age"]) >= 25) {
newData.push(item);
}
})
console.log(newData);
(六)、
lodash.js----------
var data = [{
"id": "1",
"name": "小刘",
"age": "25"
},
{
"id": "2",
"name": "小华",
"age": "24"
},
{
"id": "3",
"name": "小佳",
"age": "27"
},
{
"id": "4",
"name": "小王",
"age": "24"
},
{
"id": "5",
"name": "小何",
"age": "22"
},
];
var newData = _.filter(data, function(item) {
return item.age > 25;
})
console.log(newData)
8.