简体   繁体   English

将新对象添加到对象数组javascript

[英]adding new object to an object array javascript

I solved this. 我解决了 you are right I was running a function which was the problem. 您是对的,我正在运行一个出现问题的功能。 If anyone wants to look at the solution I have attached it below. 如果有人想看一下解决方案,请在下面附上。 This was never meant to look nice just function. 这绝不是为了使功能看起来好。 Thank you for at least looking at it. 至少谢谢您的关注。 And to answer question more clearly. 并更清楚地回答问题。 This was a debugging assignment as I am working towards a complete understanding through school. 这是调试工作,因为我正在努力通过学校全面了解。 As I said I wasn't looking for anyone to solve for me just to guide me so I can solve myself. 正如我说的那样,我并不是在寻找任何人来解决我,只是为了指导我,以便我自己解决问题。 Most people I have dealt with in the developer community has been super helpful so I simply wanted to reach out for help in my new endeavors. 在开发人员社区中与我打交道的大多数人都非常有帮助,所以我只是想在我的新工作中寻求帮助。 Thank you all for that help. 谢谢大家的帮助。 Again I have fully solved all 10 errors which is what the // error# was about and believe will receive 100%. 再次,我已经完全解决了所有10个错误,这是//错误号即将发生的情况,并相信它将收到100%的错误。 I know debugging probably seems beginner and mundane but we were all beginners once. 我知道调试似乎是初学者,但我们都是初学者。 Again the link to the solved project is below with a copy of the solved and unsolved project if anyone wants to play. 同样,如果有人想玩,则下面还有指向已解决项目的链接,其中包含已解决和未解决项目的副本。 This way I offer a benefit to the question having been asked. 这样,我可以为提出的问题提供帮助。

https://www.dropbox.com/sh/vngg7j478h6zgcv/SVZF1bep6w https://www.dropbox.com/sh/vngg7j478h6zgcv/SVZF1bep6w

object array: 对象数组:

var flavors = [
{
    "flavor": "Vanilla",
    "favorite": "Yes!",
    "notes": "Great for sundaes."
},
{
    "flavor": "Chocolate",
    "favorite": "Yes!",//first error
    "notes": "All parts chocolate. What's not to love?"
},
{
    "flavor": "Neapolitan",
    "favorite": "No",
    "notes": "I dislike the strawberry chunks."
}
];

code to push the form data to the array: 将表单数据推送到数组的代码:

var saveFlavor = function(){

var fav = getFavorite();   
//error 5 not fixed
var newFlavor = function() {
    flavors.flavor = document.getElementById('flavor').value,
    flavors.favorite = fav, //error 6 not fixed 
    flavors.notes = document.getElementById('notes').value //error 7
    };
    flavors.push(newFlavor);
    console.log(newFlavor)

    location.href="#home";
    document.getElementById('list').innerHTML = "";

};
var save = document.getElementById("submitFlavor"); //error 8
save.addEventListener("click", saveFlavor);

Data called on with: 调用数据:

var showFlavors = function(){
for(var i=0, len=flavors.length; i<len; i++){ //error 2
    var newLi = document.createElement('li');   
    document.getElementById('list').appendChild(newLi);
      var heading =document.createElement('h3');
          heading.innerHTML = flavors[i].flavor;
          newLi.appendChild(heading);
  var pNotes = document.createElement('p');
          pNotes.innerHTML= flavors[i].notes;
          newLi.appendChild(pNotes);
  var pFav = document.createElement('p');
        if(flavors[i].favorite == "Yes!") {
            flav = "Yes";
            } else if (flavors[i].favorite == "No") {
            flav = "No";
    };
    console.log(flav)
    pFav.innerHTML= flav; //error 3 
    newLi.appendChild(pFav);
    console.log(newLi); 
};


};

It does push to the Array but returns as Undefined I also dropboxed this code here if you need to see more. 它确实会推送到Array,但返回Undefined,如果您需要查看更多内容,也可以在此处将此代码放入框。 https://www.dropbox.com/sh/ee0usbthykoqllg/cZi_K6zst2 https://www.dropbox.com/sh/ee0usbthykoqllg/cZi_K6zst2

I fixed everything else in this debugging process however I am having trouble solving why I am returning undefined. 我在调试过程中修复了所有其他问题,但是我无法解决为什么返回未定义的问题。 I am not looking at someone giving me the answer, I am looking for guidance as to what I am doing wrong. 我不是在找一个给我答案的人,我正在寻找有关我做错事情的指导。 It did not work at all when I started and I have fixed the rest just need help with this. 当我开始时它根本没有用,我已经修复了其余的,只是需要帮助。 Thanks. 谢谢。

newFlavor isn't an object, it's a function. newFlavor不是对象,而是函数。 This is how you create an object: 这是创建对象的方式:

var newFlavor = {
    flavor: document.getElementById('flavor').value,
    favorite: fav, //error 6 not fixed 
    notes: document.getElementById('notes').value //error 7
};

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

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