示例代码:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>eslint for...in 报错处理</title>
</head>
<body>
<script src="https://cdn.bootcss.com/lodash.js/4.17.10/lodash.min.js"></script>
<script type="text/javascript"> Array.prototype.contains = function(item) { return this.indexOf(item) >= 0 } const arr = [1, 2, 3, 4] for(let key in arr) { //使用hasOwnProperty 处理一下
if(arr.hasOwnProperty(key)) { console.log(key) } } for(let [key, value] of Object.entries(arr)) { console.log(key + ' ' + value) } </script>
</body>
</html>
解决方法:
(1)hasOwnProperty
(2)for of