数组循环以及新增部分:
ES5里面新增一些东西循环:
1. for
for(let i=0; i<arr.length; i++)
2. while
arr.forEach() // 代替普通for
arr.forEach(function(val, index, arr){
console.log(val, index, arr);
});
arr.map()
// 非常有用,做数据交互 "映射"
//正常情况下,需要配合return,返回是一个新的数组
//若是没有return,相当于forEach
注意:平时只要用map,一定是要有return
重新整理数据结构:
[{title:‘aaa’}] -> [{t:‘aaaa’}]
let arr = [
{
title:'aaaaa', read:100, hot:true},
{
title:'bbbb', read:100, hot:true},
{
title:'cccc', read:100, hot:true},
{
title:'dddd', read:100, hot:true}
];
let newArr = arr.map((item, index, arr)=>{
let json={
}
json.t = `^_^${
item.title}-----`;
json.r = item.read+200;
json.hot = item.hot == true && '真棒!!!';
return json;
});
console.log(newArr);
//0: {t: "^_^aaaaa-----", r: 300, hot: "真棒!!!"}
//1: {t: "^_^bbbb-----", r: 300, hot: "真棒!!!"}
//2: {t: "^_^cccc-----", r: 300, hot: "真棒!!!"}
//3: {t: "^_^dddd-----", r: 300, hot: "真棒!!!"}
arr.filter(): 过滤,过滤一些不合格“元素”, 如果回调函数返回true,就留下来
let arr = [
{
title: