首页 > 解决方案 > 函数无法识别来自另一个函数的条件

问题描述

我有这个功能:

function checkDuplicity(){
  for (var i = 0; i < newCombination.length; i++) {
    if(JSON.stringify(newCombination[i]) == localStorage.getItem("test")){
      return false
    }
  }
  return true
}

当我在 DevTools 中检查此函数时,它会在满足或不满足条件时返回正确的布尔值。

我在这里使用这个函数作为执行函数的条件

function makeId(){
  if(storedDate != null){
    if (checkDuplicity()) {
      localStorage.setItem("combination"+(localStorage.length+1), JSON.stringify({ 
          movie: storedMovie,
          date: storedDate,
          time: storedTime
      }));
    }
}}

即使我在 DevTools 中检查了 checkDuplicity() 的输出并且它的错误 - 上面的函数仍然被执行。

谢谢

这是 newCombination 的填充方式:

var newCombination = []

function pushingInForm() {
  for (var i = 1; i <= localStorage.length; i++) {
    if ((localStorage.getItem('combination' + i) != null)) {
      var push = JSON.parse(localStorage.getItem("combination" + i));
      newCombination.push(push);
    } else {
      //
    }

标签: javascript

解决方案


推荐阅读