ga-wdi-exercises / project1

[project] GA- Project 1
3 stars 75 forks source link

adding Object to array #200

Closed ghost closed 9 years ago

ghost commented 9 years ago
var saveDeck = function(){
  var saveName = $("#savename").val;
  var question = $(".add > .question");
  var answer = $(".add > .answer");
  var dropDown= $(".dropdown");
  var tempDeck = jQuery.extend(true, {}, cardDeck);
  var deckToSave = {saveName: tempDeck};
  decksMaster.unshift(deckToSave);
  cardDeck=[]
  question.val("");
  answer.val("");
  var cardCount = Object.keys(cardDeck).length;
  currentCard = 0;
  popLoadMenu()
}

I was hoping to add an object (cardDeck) to an array (decksMaster) AS an object(deckToSave) with the key as the variable the user inputs(saveName). However, I think the key is just becoming the string "saveName"?

RobertAKARobin commented 9 years ago

Yeah, Javascript's a little weird this way. There isn't a way to do what you want to do in the way you're doing it.

Instead, try this:

var deckToSave = {}
deckToSave[saveName] = tempDeck;
ghost commented 9 years ago

hmm, now instead of saving the string it seems to be saving the jquery function of "$("#savename").val;"

in the console: function (a){var b,c,d,e=this[0];{if(arguments.length)return d=n.isFun ....

ghost commented 9 years ago

Ah figured it out needed the parens'

Thanks very much!

RobertAKARobin commented 9 years ago

:+1: