javascript - How to print whole list in for loop?
问题描述
I just want to practice Javascript, so I try this easy code. In my expected that the output should be whole list.
When I use this code I only can get the output is
[5, 9, 17, 14, 4, 19, 11, 8, 13, 10, 18, 15, 16, 20]
I did not know what happened on it, and where was
[1,0,2,3,6,7,12...]
var li = [5,9,17,14,1,0,4,19,11,6,8,13,10,2,18,15,16,3,12,7,20]
var length = li.length;
var x = [];
for(var i = 0; i < length; i++){
if(!(li[i]in x)){
x.push(li[i]);
};
}
console.log(x);
解决方案
the condition checking if(!(li[i]in x)){
is not correct. in
check if keys(index) exist in the array .
Change to if(!x.includes(li[i])){
推荐阅读
- java - 如何使用 Java Spring Boot 验证用户注册表单?我收到一条错误消息,说 NoMessageFound 错误
- reactjs - 反应路由器:移动到 / 时要定义预期的“状态”
- javascript - 注册新用户的最佳方式是什么 - 获取与发布
- javascript - 无法在 XMLHttpRequest 响应中加载 JavaScript 函数
- python - 是否可以在 GridSearchCV() 中同时使用 MLPRegressor() 和 make_pipeline()?
- spring - 在“mvn clean install”期间的功能测试之前,Wiremock 不会启动
- android - 在不卸载 [INSTALL_FAILED_UPDATE_INCOMPATIBLE] 的情况下将 Android Native 应用程序更新到 Flutter
- javascript - 如何理解 react-three-fiber 中的“useBox”
- blockchain - 无论采用哪种方法,我都无法在 VSCode 中导入 Open Zeppelin 合约
- c - GDB 错误 - 不是可执行格式:文件格式无法识别