遍历数组 ES5 写法
arr.forEach(function(value,index){
console.log(index+'--'+value);
})
遍历数组 ES6 写法
for( let i of arr){
console.log(i);
}
判断字符串str是否以1开头
str.indexOf('1') == 0
从集合中过滤符合条件的元素
var _ = require('lodash'); var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false } ]; var rtnArr = _.filter(users, function(o) { return !o.active; }); console.log(rtnArr); // [ { user: 'fred', age: 40, active: false } ]
判断数组中是否包含指定的字符串
var fruits = ["Banana", "Orange", "Apple", "Mango"]; if(fruits.indexOf("Apple") > -1){ console.log('数组中包含Apple') }else{ console.log('数组中不包含Apple') }
对数组元素排序并拼接成字符串
var arr = ["morris","20180823085422","715413099"]; var value = arr.sort();var value = value.join(""); console.log(value);
json和js对象相互转换
var foo = { "a":123, "b":456 } //js对象转为json var jsonStr =JSON.stringify(foo); console.log(jsonStr) //{"a":123,"b":456} //json转为js对象 var bar = JSON.parse(jsonStr); console.log(bar.a); //123 console.log(bar.b); //456