3 JS 遍历方法题

1 遍历对象的方法

1 遍历对象: for in 循环
-----------------------------------------------------------------------------------------
1 注意事项: 
  1 for in 只会遍历自定义属性, 不会遍历原型上的默认属性和方法
  2 但在实际应用中, 若原型上新增了属性和方法, for in 就会把新增的属性和方法遍历出来 (解决方案, 如下代码)
  3 for in 循环也可遍历数组, 但一般不那么干


2 代码范式: 

  1 基础格式
    for (var k in obj) {
      console.log(k)   //obj的每个属性
      console.log(obj[k])   //obj的每个属性对应的属性值
    }

  2 解决方式 1
    for (var k in obj) {
      if(!Object.prototype.hasOwnProperty.call(people,key)) continue;
      console.log(k, obj[k])   //如上代码就, 解决了, 遍历新增属性和方法的这个特性
    }

  3 解决方式 2
    for (var k in obj) {
      if (obj.hasOwnProperty(k)){  //此方法会忽略掉, 从原型上得到的属性
         console.log(k, obj[k]) 
      }
    }
-----------------------------------------------------------------------------------------

2 遍历数组的方法

1 for of 循环
-----------------------------------------------------------------------------------------
1 注意事项: 
  1 无法遍历对象
  2 只会遍历数组元素

2 代码范式: 
  let arr = [1, 2, 3]
  for (let v of arr) {
    console.log(v)   // v == 数组元素
  }
-----------------------------------------------------------------------------------------

2 forEach()  //操作
-----------------------------------------------------------------------------------------
1 注意事项: 
  1 没有返回值
  2 可以改变原数组

2 使用场景: 
  1 在可以改变原数组的情况下使用
  2 所以不需要有返回值

3 代码范式: 
  let arr = [1, 2, 3]
  arr.forEach( (v, i) => console.log(v + '---' + i) )
-----------------------------------------------------------------------------------------

3 map()  //操作
-----------------------------------------------------------------------------------------
1 注意事项: 
  1 有返回值
  2 不可以改变原数组

2 使用场景: 
  1 不能改变原数组的时候使用
  2 这样就可以, 生成我们需要的新数组,return 出来

3 代码范式: 
  let arr = [1, 2, 3]
  let hh = arr.map( (v, i) => v + i )
  console.log(hh)
-----------------------------------------------------------------------------------------

4 filter  //过滤
-----------------------------------------------------------------------------------------
1 作用: 过滤数组, 拿到我们需要的数组元素, 组成新的数组,return 出来

2 代码范式: 
  let arr = [1, 2, 3]
  let hh = arr.filter( v => v>2 )
  console.log(hh)
-----------------------------------------------------------------------------------------

5 every  //判断
-----------------------------------------------------------------------------------------
1 作用: 遍历所有数组元素, 只要存在一个不符合条件的, 就返回 false

2 代码范式:
  let arr = [1, 2, 3]
  var hh = arr.every(v => v>2)  //只要发现一个, 不大于2的数组元素, 就返回 false
  console.log(hh)  // false
-----------------------------------------------------------------------------------------

6 some  //判断
-----------------------------------------------------------------------------------------
1 作用: 遍历所有数组元素, 只要存在一个符合条件的, 就返回 true

2 代码范式:
  let arr = [1, 2, 3]
  var hh = arr.every(v => v>2)  //只要发现一个, 大于2的数组元素, 就返回 true
  console.log(hh)  // true
-----------------------------------------------------------------------------------------

3 各种遍历方法之间的区别

1 for in / for of --> 区别

比较项for infor of
作用遍历对象遍历数组
功能可以遍历属性不能遍历属性

2 forEach 和 for循环的区别 -> for 循环更加灵活

forEachfor
不支持 break操作 (报错)不报错
无法在遍历的同时删空自己可以
不能控制循环的起点, 只能从0开始可以自定义, 遍历起点
for 循环更加灵活, 可操作性强forEch 使用方便, 封装好的 API
1 总结
-----------------------------------------------------------------------------------------
1 通常 for循环就可以解决我们的问题了

2 但是很多情况下, 使用 API可以更方便的解决问题
-----------------------------------------------------------------------------------------
首先,需要准备一个包含考试试的数据结构。假设我们有以下试数据: ```javascript const questions = [ { question: "1 + 1 = ?", options: ["1", "2", "3", "4"], answer: 1 }, { question: "2 + 2 = ?", options: ["3", "4", "5", "6"], answer: 1 }, // ... ]; ``` 其中每个试包括一个问描述、若干选项以及正确答案的索引。 然后,我们可以使用JavaScript动态生成HTML页面来展示考试试。例如,我们可以使用以下代码来展示第一道试: ```javascript const currentQuestion = 0; // 当前展示的试索引 const questionContainer = document.createElement("div"); // 展示问描述 const questionTitle = document.createElement("h3"); questionTitle.textContent = questions[currentQuestion].question; questionContainer.appendChild(questionTitle); // 展示选项列表 const optionsList = document.createElement("ul"); for (let i = 0; i < questions[currentQuestion].options.length; i++) { const option = document.createElement("li"); option.textContent = questions[currentQuestion].options[i]; optionsList.appendChild(option); } questionContainer.appendChild(optionsList); // 将试容器添加到页面中 document.body.appendChild(questionContainer); ``` 以上代码将生成一个包含问描述和选项列表的HTML元素,并添加到页面中。 接下来,我们可以添加一些交互逻辑,例如添加点击事件处理程序来记录用户选择的答案,并在用户回答完所有问后显示得分。 ```javascript const currentQuestion = 0; let userAnswers = []; // 记录用户选择的答案 const questionContainer = document.createElement("div"); // 展示问描述 const questionTitle = document.createElement("h3"); questionTitle.textContent = questions[currentQuestion].question; questionContainer.appendChild(questionTitle); // 展示选项列表 const optionsList = document.createElement("ul"); for (let i = 0; i < questions[currentQuestion].options.length; i++) { const option = document.createElement("li"); option.textContent = questions[currentQuestion].options[i]; option.dataset.index = i; // 为选项添加数据属性,用于记录该选项的索引 option.addEventListener("click", () => { // 用户点击选项时记录答案 userAnswers[currentQuestion] = parseInt(option.dataset.index); showNextQuestion(); }); optionsList.appendChild(option); } questionContainer.appendChild(optionsList); // 将试容器添加到页面中 document.body.appendChild(questionContainer); function showNextQuestion() { // 判断是否回答完所有问 if (userAnswers.length === questions.length) { // 计算得分并展示 const score = calculateScore(userAnswers); const scoreContainer = document.createElement("div"); const scoreTitle = document.createElement("h3"); scoreTitle.textContent = `您的得分是 ${score} 分`; scoreContainer.appendChild(scoreTitle); document.body.appendChild(scoreContainer); } else { // 展示下一道试 const nextQuestionIndex = userAnswers.length; const nextQuestionContainer = document.createElement("div"); const nextQuestionTitle = document.createElement("h3"); nextQuestionTitle.textContent = questions[nextQuestionIndex].question; nextQuestionContainer.appendChild(nextQuestionTitle); const nextOptionsList = document.createElement("ul"); for (let i = 0; i < questions[nextQuestionIndex].options.length; i++) { const option = document.createElement("li"); option.textContent = questions[nextQuestionIndex].options[i]; option.dataset.index = i; option.addEventListener("click", () => { userAnswers[nextQuestionIndex] = parseInt(option.dataset.index); showNextQuestion(); }); nextOptionsList.appendChild(option); } nextQuestionContainer.appendChild(nextOptionsList); questionContainer.replaceWith(nextQuestionContainer); } } function calculateScore(userAnswers) { let score = 0; for (let i = 0; i < questions.length; i++) { if (userAnswers[i] === questions[i].answer) { score++; } } return score; } ``` 以上代码将为每个选项添加一个点击事件处理程序,当用户选择该选项时记录答案,并展示下一道试。当所有试都回答完毕后,将计算得分并展示在页面上。 综上所述,以上代码可以用来生成一个可以遍历显示考试试的页面。不过需要注意的是,由于考试试的复杂性和多样性,实际实现时可能需要根据具体需求进行适当的修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值