0

I am trying to use an array as a key value type scenario and it is working with the exception that every value starts with 'undefined'. I believe this is due to the initial assignment being a += operator however I am not sure how to resolve it.

This is the code stripped of a lot of string concats....

   var phasehtml = {};
        $.each(json, function (i, item) {
            phasehtml[item.Phase] += 'item:'+item.ID;
        });

Basically I am trying to append the string to the appropriate key....

6
  • Can you show the output? Commented Apr 1, 2013 at 18:03
  • is your array in json format? Commented Apr 1, 2013 at 18:04
  • Each key is undefined to begin with, you are then concatenating a string to it, thus getting undefineditem:189 Commented Apr 1, 2013 at 18:05
  • @BlueBird Note: phasehtml is an Object. It is not an Array. Commented Apr 1, 2013 at 18:05
  • 1
    @KevinB That sounds like a really scary kind of fiend! Commented Apr 1, 2013 at 18:06

3 Answers 3

3

You can change the code to only append the ID if there's already IDs:

var phasehtml = {};
$.each(json, function (i, item) {
   // Use the existing value for the phase, or an empty string that we can append to
   var existingValue = (phasehtml.hasOwnProperty(item.Phase) ? phasehtml[item.Phase] : "");
   phasehtml[item.Phase] = existingValue + 'item:' + item.ID; 
});

That's assuming that you want phasehtml to contain an appended lists of the form "item:1item:2" for each phase.

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

7 Comments

Another (potentially) cleaner way to write this: phasehtml[item.Phase] = (phaseHtml[item.Phase] || "") + 'item:'+item.ID;
This code doesn't work generically, as if (phasehtml[item.Phase]) will fail if the value is "falsy" such as 0. I realize that's not the use case here, but if we're considering use cases, I think it is extremely improbable that the OP does want item:1item:2item:3 as his final result. I know we are all guessing, but I think this guess as to his usage scenario falls short of the mark. He just needs an equal sign. In my opinion a more detailed explanation more about objects and assignments is valuable--but in text, not solely in mis-applied code.
@ColinDeClue For what it's worth just keep in mind the "falsy" part of my above comment.
@ErikE Of course, but if it starts out undefined, and he's only adding "item:1", etc, to it, then it won't be a problem. But, yes, falsy statements do fail for 0, and this type of thing should never be used for anything that could be a number.
BlueBird, do take note of Erik's comments if you don't actually intend each phase to have a concatenated list of item IDs. As well as the potential problem with "falsy" values.
|
0

The array you have posted is empty.

var phasehtml = {};

It seems that is the cause the following statement phasehtml[item.Phase] is being evaluated to "undefined".

Comments

0

Hmmm,

got the problem.

In your code you are trying to add with that value which is previously not defined that's why this error is occur.

In your code you have not initialize the variable that you are adding.

So try this:

  var phasehtml = {};
    $.each(json, function (i, item) {
        phasehtml[item.Phase] = "";
        phasehtml[item.Phase] += 'item:'+item.ID;
    });

In this first assign some value, here is blank and then use that index of array.

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.