Array.prototype.indexOf()
**indexOf()**方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。
switch case
switch (sign) {
case ORDER_SIGN[2]:
case ORDER_SIGN[3]:
return items;
default:
return '';
}
像这种情况表示情况2和3都执行返回return,而不是情况2是没写或者是没有用
instanceof
instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);
console.log(auto instanceof Car);
// expected output: true
console.log(auto instanceof Object);
// expected output: true
map的index从0开始计算
markers的巧妙获取labels
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 3,
center: { lat: -28.024, lng: 140.887 },
});
// Create an array of alphabetical characters used to label the markers.
const labels = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Add some markers to the map.
// Note: The code uses the JavaScript Array.prototype.map() method to
// create an array of markers based on a given "locations" array.
// The map() method here has nothing to do with the Google Maps API.
const markers = locations.map((location, i) => {
return new google.maps.Marker({
position: location,
label: labels[i % labels.length],
});
});
// Add a marker clusterer to manage the markers.
new MarkerClusterer(map, markers, {
imagePath:
"https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m",
});
}
labels[i % labels.length]
labels.length比i的最大值大一
效果不是相当于
labels[i]
闭包
一个函数和对其周围状态(lexical environment,词法环境)的引用捆绑在一起(或者说函数被引用包围),这样的组合就是闭包(closure)。也就是说,闭包让你可以在一个内层函数中访问到其外层函数的作用域。在 JavaScript 中,每当创建一个函数,闭包就会在函数创建的同时被创建出来。
react的行内样式
style={{ color:'red',zIndex:3}}
逻辑或判断
null || " " //" "
null || undefined //undefined
undefined || " " //" "
undefined || true //true
undefined || false //false
对象数组按要求查询某项的元素
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber));
// expected output: 3
注意箭头函数的简写方式,以下是错误写法
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => {element > 13};
console.log(array1.findIndex(isLargeNumber));
// expected output: -1
不简写如下
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => {
if(element > 11){
return true}}
console.log(array1.findIndex(isLargeNumber));
// expected output: 3
如果是对象数组,最好使用Object.is去判断
console.log(options); console.log(options.findIndex((item) => Object.is("EXPRESS", item.key)));
findIndex方法的使用,返回索引值
const arr2 = [{
"corrected": [1, 2, 3, 4],
"piece": 11,
"pieceName": "数据1",
"uncorrected": [10, 2, 3, 4]
}, {
"corrected": [1, 2, 3, 4],
"piece": 22,
"pieceName": "数据2",
"uncorrected": [55, 6, 7, 8]
}, {
"corrected": [1, 2, 3, 4],
"piece": 33,
"pieceName": "数据3",
"uncorrected": [99, 10, 11, 12]
}];
var piece = 33;
var rsIndex = arr2.findIndex((value) => Object.is(piece, value.piece))
console.log(rsIndex); // 2
debugger
function potentiallyBuggyCode() {
debugger;
// do potentially buggy stuff to examine, step through, etc.
}
Expected property shorthand报错
表示当前语法形式可以优化改写
ClustererArr.push({ lat: lat, lng: lng });
ClustererArr.push({ lat,lng });
Array.prototype.find方法
正确写法
arr.find(item => item.displayName = 'xxx')
如果写成这样
arr.find(item => {item.displayName = 'xxx'})
结果会变成undefined
Array.prototype.filter 也一样 ,can't use {},这会导致结果不准确
获取字符串最后一位
const index = event.target.id.substr(event.target.id.length - 1, 1);
'xxx'.substr('xxx'.length-1,1)
Array.prototype.findIndex()
**findIndex()**方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber));
// expected output: 3
indexOf 与 findIndex 区别
-
indexOf:查找值作为第一个参数,采用===比较,更多的是用于查找基本类型,如果是对象类型,则是判断是否是同一个对象的引用 -
findIndex:比较函数作为第一个参数,多用于非基本类型(例如对象)的数组索引查找,或查找条件很复杂