0
  var coord = $(this).find("td:eq(0)").text().match(/\d{1,3}\|\d{1,3}/g);
  var AantalSpeerDorp = parseInt($(this).find("td:eq(2)").text());
  var AantalZwaardDorp = parseInt($(this).find("td:eq(3)").text());
  var AantalBijlDorp = parseInt($(this).find("td:eq(4)").text());
  var AantalScoutDorp = parseInt($(this).find("td:eq(5)").text());
  var AantalLCDorp = parseInt($(this).find("td:eq(6)").text());
  var AantalHcDorp = parseInt($(this).find("td:eq(7)").text());
  var AantalRamDorp = parseInt($(this).find("td:eq(8)").text());
  var AantalKatDorp = parseInt($(this).find("td:eq(9)").text());
  var AantalEdelDorp = parseInt($(this).find("td:eq(10)").text());

  var eenhedenperdorp = {};
  eenhedenperdorp[coord] = coord;
  eenhedenperdorp[coord]["speer"] = AantalSpeerDorp;
  eenhedenperdorp[coord]["zwaard"]= AantalZwaardDorp;
  eenhedenperdorp[coord]["bijl"] = AantalBijlDorp;
  eenhedenperdorp[coord]["Scout"] = AantalScoutDorp;
  eenhedenperdorp[coord]["lc"] = AantalLCDorp;
  eenhedenperdorp[coord]["hc"] = AantalHcDorp;
  eenhedenperdorp[coord]["ram"] = AantalRamDorp;
  eenhedenperdorp[coord]["kata"] = AantalKatDorp;
  eenhedenperdorp[coord]["edel"] = AantalEdelDorp;

As you can see I'm trying to create this object like this (all these variables are in an each function), but when I try it out I get an empty object.

alert(JSON.stringify(eenhedenperdorp));

Result: {}

What's the correct way to do this?

Fiddle: http://jsfiddle.net/czGrv/

4
  • 2
    Move this line: var eenhedenperdorp = {}; outside of the loop? Otherwise you're just creating a new object that's not visible outside of the scope of the each function every time. Commented Feb 6, 2014 at 14:16
  • I did put that outside of the loop :) I just placed it here so it would be clear what I'm trying to do. @adeneo How is this complicated? Everything is a variable, and I'm putting every variable with the corresponding numbers and types in an object. I don't know how else I could do it? Commented Feb 6, 2014 at 14:19
  • 1
    What object is this at the start of the code? What event are you inside of? More code please. Commented Feb 6, 2014 at 14:19
  • @user3227070 why not providing more context of your posted code, as the each loop Commented Feb 6, 2014 at 14:20

2 Answers 2

2

Looks to me like you're not instantiating the object that you seem to want to instantiate. Change the first line of instantiation to:

eenhedenperdorp[coord] = {};

Your [coord] parameter needs to be an object itself to fill it with parameters later on in your code.

Sign up to request clarification or add additional context in comments.

6 Comments

That does the trick, you're a life saver :) Thx alot! I'll accept in one minute btw
No problemo. Add some nice camel casing, it's valid in dutch too! eenhedenPerDorp
Now if you just data-drive the lookup in a loop so their code is not so long-winded... :)
@TrueBlueAussie I am not actually completely sure what 'data-drive' means here... Although I'd be open to adding some things in my answer to make the code less long-winded. What is your proposition?
He probably means getting eq(1-10) in a loop, but I think it's nice and clear this wzy :)
|
0

Check what you want to do.

Since you use .match(), coord will be either an array or null (if the match isn't good).

Your line:

eenhedenperdorp[coord] = coord;

ensures that your following object property assignments will fail, either silently (you can't do that to arrays) or violently (with a TypeError).

You probably meant:

eenhedenperdorp[coord] = {};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.