简体   繁体   中英

Adding new pair of objects into object array, vanilla is

I need to push the value of the prompts into the array, with keys (rest of code depends on the keys being present and correct). I can't seem to push the additions into the array.

  var cWords = [
                        {word: "Hot", antonym: "Cold"},
                        {word: "Sweet", antonym: "Sour"} 
                        ];


btnAdd.eventListener("click", function (){
      var inputC = prompt("Add your word here");
      var inputA = prompt("Add corresponding word");

        var ac = ({word: + inputC} + {antonym: inputA});
        cWords.push(ac);

Can't seem to add successfully, getting the output [object: object] into the array.

If theres any typos, it's due to autocorrect on mobile. All syntax correct

The syntax you use is kinda weird... no need for the parenthesis and those + you put there in the middle. Just constract the object like the ones in the array:

var ac = { word: inputC, antonym: inputA };
cWords.push(ac);

You cant assign an object like this: var ac = ({word: + inputC} + {antonym: inputA});

Try this instead:

var ac = {word: inputC, antonym: inputA};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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