lodash常用函数一
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>lodash常用函数</title>
</head>
<body>
<script src="//cdn.bootcss.com/lodash.js/4.16.1/lodash.min.js"></script>
<p>1.for 循环 _.times()</p>
<script>
/*for(let i = 0 ;i <5; i ++){
//
}*/ _.times(5,function (i) {
//
console.log("times: "+i);
})
</script>
<p>2. json 取数据 _.map()</p>
<script>
var ownerArr = [
{
"owner":"aa",
"pets":[{"name":"dog1"},{"name":"dog2"}]
},
{
"owner":"bb",
"pets":[{"name":"dog11"},{"name":"dog12"}]
}, ];
// 1
ownerArr.map(function (owner) {
return owner.pets[0].name;
});
// 2
console.log("json: "+ownerArr.map(owner => owner.pets[0].name));
//3
console.log('josn2: ' + _.map(ownerArr,'pets[0].name'))
</script>
<p>3. es6 map复习 map相当于 for 循环 </p>
<script> console.log("map1: "+[1, 2, 3].map(x => x * 2)); let arr = [4,5,60].map(x=>x*3);
console.log("map2: "+arr);
var arr2 = [6,7,8]; for(var i = 0 ;i<arr2.length;i++){
console.log('arr2: ' + arr2[i]*2);
}
</script>
</body>
</html>