由some()方法返回的未定义

时间:2019-05-21 14:00:57

标签: javascript

我正在尝试防止具有一堆将包含对象的数组的对象具有重复的对象(我知道它看起来很奇怪,但是尝试在下面的代码中您将理解我的意思)

JavaScript

var UIController = (function() {

  var DOMstrings = {
    inputDay: ".optionList",
    inputTimeF: ".inputTime",
    inputTimeT: ".inputTime2",
    inputText: ".inputText",
    goingToCkecked: ".checkboxx",
    inputBtn: ".add__btn",
    planContainer: ".container",
    errorCase: ".error-case",
    optionList: ".optionList",
    optionListId: "#optionList",
    errorDes: "error-description",
  };


  return {
    getInput: function() {
      return {
        inputDay: document.querySelector(DOMstrings.inputDay).value,
        inputTimeF: document.querySelector(DOMstrings.inputTimeF).value,
        inputTimeT: document.querySelector(DOMstrings.inputTimeT).value,
        inputText: document.querySelector(DOMstrings.inputText).value,
        goingToCkecked: document.querySelector(DOMstrings.goingToCkecked).checked,


      };
    },
    getDOMstrings: function() {
      return DOMstrings;
    },



  };

})(); //END OF THE UICONTROLLER MODULE




var internalController = (function(UICtrl) {

  var Plan = function(id, from, to, text, goingToCkecked) {
    this.id = id;
    this.from = from;
    this.to = to;
    this.text = text;
    this.goingToCkecked = goingToCkecked;
  };

  var data = {
    Monday: [],
    Tuesday: [],
    Wednesday: [],
    Thursday: [],
    Friday: [],
    Saturday: [],
    Sunday: []
  };

  function hasObject( day, object ) {
      const dataset = data[day];

// console.log(object.inputTimeF);
      return dataset.some(el => {
          return (
            console.log(el.inputTimeF)
//el.inputTimeF=== object.inputTimeF
              // el.to === object.to
              // el.txt === object.txt
          );
      });
  }

  var Dom = UICtrl.getDOMstrings();

return {


    addItem: function(day, from, to, text, goingToCkecked) {
      var newPlan, ID;


      //CREATE NEW ID
      if (data[day].length > 0) {
        ID = data[day][data[day].length - 1].id + 1;
      } else {
        ID = 0;
      }

      //CREATE NEW PLAN BASED ON THE PICKED DAY

      if (day === "Monday" || day === "Tuesday" || day === "Wednesday" || day === "Thursday" || day === "Friday" || day === "Saturday" || day === "Sunday") {
        newPlan = new Plan(ID, from, to, text, goingToCkecked);
      }



      //PUSH IT INTO OUR DATA STRUCTURE
      data[day].push(newPlan);

      //RETURN THE NEW ELEMENT
      return newPlan;

    }, //END OF THE ADD ITEM METHOD


    duplicatedObject: function(day,object) {
      return hasObject(day,object);
    }


  }; //end of the return object of the internal controller module


})(UIController);





var controller = (function(interCtrl, UICtrl) {
  var input, newPlan, DOM;

  DOM = UICtrl.getDOMstrings();

  function setupEventListeners() {

    document.querySelector(DOM.inputBtn).addEventListener("click", ctrlAddPlans);

    document.addEventListener("keypress", function(e) {
      if (e.keyCode === 13) {
        document.activeElement.blur();
        ctrlAddPlans();
      }
    });

  }

  var ctrlAddPlans = function() {


    //3.get the filed input data
    input = UICtrl.getInput();
    // console.log(input);


    //5.add the plan to the internalController
    newPlan = interCtrl.addItem(input.inputDay, input.inputTimeF, input.inputTimeT, input.inputText, input.goingToCkecked);

    // 4.Refuse duplicated plans
    var res = interCtrl.duplicatedObject(input.inputDay, input);
    console.log(res)

  };

  return {
    init: function() {
      console.log('the app has started');
      setupEventListeners();
    },

  };
})(internalController, UIController);


controller.init();

那个大对象是data对象,您看到的是他有一堆arrays,这些数组将包含用户输入的某些输入中的objects

因此,当我尝试解决此问题(我不想让任何数组具有重复的对象)时,我正在使用some()方法(尝试在上面的代码中查看has object函数)

我正在比较来自参数的objectel的{​​{1}}参数,但是我总是得到from作为返回值,所以当我尝试调试我的代码时,我发现false返回未定义

屏幕截图 enter image description here

我用谷歌搜索,但没有发现有用的东西

1 个答案:

答案 0 :(得分:0)

首先,确保el.inputTimeF不是undefined,然后您可以像下面这样修改代码:

return dataset.some(el => {
     return el.inputTimeF;
});

最后,请勿返回console.log,因为它将返回undefined