简体   繁体   English

将对象添加到数组onclick Javascript

[英]Adding object to array onclick 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); }; 

I am trying to push the object that I am creating into an array. 我正在尝试将要创建的对象推送到数组中。 I can console.log the object when the button is clicked, but not return anything out of the click function, I know I missing something basic, but couldn't find out what. 当单击按钮时,我可以console.log该对象,但不会从click函数中返回任何内容,我知道我缺少一些基本的东西,但是找不到。

Fix your selectedAge data (closing quote) and return the new value, not the result of the push. 修正您的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;

};

You have Unterminated string constant here , var selectedAge = "EaxampleData; 您这里有Unterminated字符串常量,var selectedAge =“ EaxampleData;

insert the closing bracket and try again 插入右括号,然后重试

  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> 

here is the working example https://codepen.io/kamindu/pen/PdwagP?editors=1111 这是工作示例https://codepen.io/kamindu/pen/PdwagP?editors=1111

Shift the responsibility of pushing data to the householdMember by creating another function 通过创建另一个功能将将数据推送到householdMember的责任

Instead of loging the value you can also return it 除了记录值,您还可以返回它

 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> 

The problem with your code is that you are returning the return value of JavaScript push function. 代码的问题在于,您正在返回JavaScript push函数的返回值。 This returns the length of the array. 这将返回数组的长度 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

What you need to do is first add object to array and then return the array. 您需要做的是首先将对象添加到数组, 然后返回该数组。

householdData.push(currentHouseholdMember);
return householdData;

I am trying to push the object that I am creating into an array. 我正在尝试将要创建的对象推送到数组中。

It is being pushed, just confirmed your code works 它被推送,只是确认您的代码有效

I can console.log the object when the button is clicked, but not return anything out of the click function 我可以在单击按钮时console.log该对象,但不会从click函数中返回任何内容

You are passing to console log just the newly created object, to see the array updated try printing the updated array in a console.log like this working example: 您只将新创建的对象传递到控制台日志,以查看更新后的数组,请尝试在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> 

Also realize there is a semicolon missing here in your code 还要意识到代码中缺少分号

var selectedAge = "EaxampleData <--;

Your logic of passing a callback to click event, then creating new object passing parameters to householdMember function using the new operator to instantiate an object, then pushing new object to array is correct. 您传递以下事件的逻辑:单击事件的回调,然后使用new运算符实例化一个对象,然后将新对象推入数组,然后将参数传递给HouseholdMember函数,以创建新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM