reduce的用法
定义:对数组中的每个元素执行一个自定义的累计器,将其结果汇总为单个返回值
形式:
array.reduce((t, v, i, a) => {}, initValue)
参数 callback:回调函数(必选)
initValue:初始值(可选)
回调函数的参数 total(t):累计器完成计算的返回值(必选)
value(v):当前元素(必选)
index(i):当前元素的索引(可选)
array(a):当前元素所属的数组对象(可选)
删除属性的唯一方法是使用 delete 操作符;设置属性为 undefined 或者 null 并不能真正的删除属性, 而仅仅是移除了属性和值的关联。
function foo(a, b, c) {}
var bar = {};
foo.apply(bar, [1, 2, 3]); // 数组将会被扩展,如下所示
foo.call(bar, 1, 2, 3); // 传递到foo的参数是:a = 1, b = 2, c = 3
"" == "0" // false
0 == "" // true
0 == "0" // true
false == "false" // false
false == "0" // true
false == undefined // false
false == null // false
null == undefined // true
" \t\r\n" == 0 // true
{} === {}; // false
Object.prototype.toString.call([]) // "[object Array]"
Object.prototype.toString.call({}) // "[object Object]"
Object.prototype.toString.call(2) // "[object Number]"
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call(undefined) // "[object Undefined]"
typeof foo !== 'undefined'
上面代码会检测 foo 是否已经定义;如果没有定义而直接使用会导致 ReferenceError 的异常。 这是 typeof 唯一有用的地方。
instanceof 操作符用来比较两个操作数的构造函数
天地图查询行政区坐标
locateZoneByName(code) {
const that = this
// 156000000
const postStr = JSON.stringify({
'searchWord': '156' + code, 'searchType': '0', 'needSubInfo': 'false', 'needAll': 'false', 'needPolygon': 'false', 'needPre': 'true'
})
const url = 'http://api.tianditu.gov.cn/administrative?postStr=)' + postStr + '&tk=' + TDTKEY
axios.get(url).then((res) => {
const district = res.data.data
const center = [district.lnt, district.lat]
let zoom = map.getZoom()
switch (district.adminType) {
case 'province': zoom = 5; break
case 'city': zoom = 7; break
case 'county': zoom = 9; break
case 'street': zoom = 11; break
}
})
},
// 创建数组时:下面的代码将会使人很迷惑
new Array(3, 4, 5); // 结果: [3, 4, 5]
new Array(3) // 结果: [],此数组长度为 3
网友评论