将对象添加到数组onclick Javascript

时间:2018-08-22 04:57:46

标签: javascript

var householdData = [];

function householdMember(age, rel, smoker) {
  this.age = age;
  this.rel = rel;
  this.smoker = smoker;
}

addBtn.addEventListener("click", addHouseholdMember);
  
function addHouseholdMember() {

      var selectedAge = "EaxampleData;
      var selectedRel = "ExampleData";
      var selectedText = "ExampleData";
      var selectedSmoker = "ExampleData";
  
      var currentHouseholdMember = new householdMember(selectedAge, selectedText, selectedSmoker);
      console.log(currentHouseholdMember);
      return householdData.push(currentHouseholdMember);
  
};

我正在尝试将要创建的对象推送到数组中。当单击按钮时,我可以console.log该对象,但不会从click函数中返回任何内容,我知道我缺少一些基本的东西,但是找不到。

5 个答案:

答案 0 :(得分:0)

修复您的selectedAge数据(右引号)并返回新值,而不是推送结果。

function addHouseholdMember() {

  var selectedAge = "EaxampleData";
  var selectedRel = "ExampleData";
  var selectedText = "ExampleData";
  var selectedSmoker = "ExampleData";

  var currentHouseholdMember = new householdMember(selectedAge, selectedText, selectedSmoker);
  console.log(currentHouseholdMember);
  householdData.push(currentHouseholdMember);
  return currentHouseholdMember;

};

答案 1 :(得分:0)

您在这里有Unterminated字符串常量,  var selectedAge =“ EaxampleData;

插入右括号,然后重试

  var householdData = [];
var addBtn = document.querySelector('.addBtn');
function householdMember(age, rel, smoker) {
  this.age = age;
  this.rel = rel;
  this.smoker = smoker;
}

addBtn.addEventListener("click", addHouseholdMember);

function addHouseholdMember() {
      var selectedAge = "EaxampleData";
      var selectedRel = "ExampleData";
      var selectedText = "ExampleData";
      var selectedSmoker = "ExampleData";

      var currentHouseholdMember = new householdMember(selectedAge, selectedText, selectedSmoker);
  householdData.push(currentHouseholdMember);
console.log(householdData);
};
<a class = 'addBtn'>add</a>

这是工作示例https://codepen.io/kamindu/pen/PdwagP?editors=1111

答案 2 :(得分:0)

通过创建另一个函数将将数据推送到householdMember的责任

除了记录该值,您还可以返回它

var addBtn = document.getElementById('btn')
var householdData = [];

function householdMember(age, rel, smoker) {
  this.age = age;
  this.rel = rel;
  this.smoker = smoker;
  this.addVal = function() {
    householdData.push({
      age: this.age,
      rel: this.rel,
      smoker: this.smoker
    })
    return householdData;
  }
}

addBtn.addEventListener("click", addHouseholdMember);

function addHouseholdMember() {

  var selectedAge = "EaxampleData";
  var selectedRel = "ExampleData";
  var selectedText = "ExampleData";
  var selectedSmoker = "ExampleData";

  var currentHouseholdMember = new householdMember(selectedAge, selectedText, selectedSmoker);
  console.log(currentHouseholdMember.addVal())
};
<button id='btn'>Click</button>

答案 3 :(得分:0)

代码的问题是您正在返回JavaScript C函数的返回值。这将返回数组的长度https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

您需要做的是先将对象添加到数组,然后然后返回数组。

push

答案 4 :(得分:0)

  

我正在尝试将要创建的对象推送到数组中。

正在推送,只是确认您的代码有效

  

单击按钮时,我可以console.log对象,但是不能   从点击功能中返回任何内容

您只是将新创建的对象传递到控制台日志,以查看更新的数组,请尝试在console.log中打印更新的数组,如下所示:

var householdData = [];

function householdMember(age, rel, smoker) {
  this.age = age;
  this.rel = rel;
  this.smoker = smoker;
}

document.getElementById("button").addEventListener("click", addHouseholdMember);

function addHouseholdMember() {
      var selectedAge = "28";
      var selectedRel = "25";
      var selectedText = "22";
      var selectedSmoker = "21";
      var currentHouseholdMember = new householdMember(selectedAge, selectedText, selectedSmoker);
      console.log(currentHouseholdMember);
      console.log(householdData);
      return householdData.push(currentHouseholdMember);
     
};
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <button id="button">CLICK</button>
</body>
</html>

还意识到您的代码中缺少分号

var selectedAge = "EaxampleData <--;

您传递以下信息的逻辑:单击事件的回调,然后使用new运算符实例化一个对象,然后将新对象推到数组中,以创建新的对象将参数传递给HouseholdMember函数。