Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have an array called viewObject.terms. This array can contain up to 3 items, I need to iterate through this array and save the values into a data object.

ie:

data.term_id_1 = viewObject.terms[0].term_id
data.ticker_1  = viewObject.terms[0].its_ticker

This is what I first tried, but I get the error: Cannot set property '0' of undefined

for (var i=0; i<viewObject.terms.length; i++) {
    data.term_id_[i+1] = viewObject.terms[i].term_id;
    data.ticker_[i+1]  = viewObject.terms[i].its_ticker;
}

TypeError


So for the moment, I'm just using a switch case to handle this currently (albeit in a super ugly way):

if (viewObject.terms.length > 0) {
    switch(viewObject.terms.length) {
        case 1:
            data.term_id_1 = viewObject.terms[0].term_id;
            data.ticker_1  = viewObject.terms[0].its_ticker;
            break;
        case 2:
            data.term_id_1 = viewObject.terms[0].term_id;
            data.ticker_1  = viewObject.terms[0].its_ticker;
            data.term_id_2 = viewObject.terms[1].term_id;
            data.ticker_2  = viewObject.terms[1].its_ticker;
            break;
        case 3:
            data.term_id_1 = viewObject.terms[0].term_id;
            data.ticker_1  = viewObject.terms[0].its_ticker;
            data.term_id_2 = viewObject.terms[1].term_id;
            data.ticker_2  = viewObject.terms[1].its_ticker;
            data.term_id_3 = viewObject.terms[2].term_id;
            data.ticker_3  = viewObject.terms[2].its_ticker;
            break;
    }
}
share|improve this question
    
The desire to improve code is implied for all questions on this site. Question titles should reflect the purpose of the code, not how you wish to have it reworked. See How to Ask. – Jamal Oct 13 at 21:41
    
as phrased, this seems more akin to a stackoverflow type question, because you're receiving an error (exception)... but it's likely answered already, and so it'll just be flagged as a duplicate. – Brett Caswell Oct 14 at 0:48

2 Answers 2

up vote 3 down vote accepted

Another solution to the problem could be expressed using Array.prototype.forEach(), as the following:

viewObject.terms.forEach(function(term, index){
    data['term_id_' + (index + 1)] = term.term_id;
    data['ticker_'  + (index + 1)] = term.its_ticker;
});
share|improve this answer
    
This one is more elegant than the author's own conclusion. votes++; :-) – KingCodeFish Oct 14 at 0:12
    
yeah, that's one way of doing it, but the link you referenced actually provides a sample on how to make a CopyTo function on objects.. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – Brett Caswell Oct 14 at 0:56

Just figured this out! Hope my answer helps someone else:

for (var i=0; i<viewObject.terms.length; i++) {
    data['term_id_'+(i+1)] = viewObject.terms[i].term_id;
    data['ticker_'+(i+1)]  = viewObject.terms[i].its_ticker;
}

This is how you dynamically name and iterate keys inside of an object

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.