1

I have a dynamic array and I am trying to increment the value by 1 if the key exists in the array. According to my debug it is incrementing the key and and creating a second key/value pair.

A snippet of my code:

        for (var i = 0; i < choices.length; i++) {
            console.log(choices[i]);
            if (choices[i].YearTermId == 1) {
                if (!lookup(firstChoice, choices[i].FirstChoiceOptionId)) {
                    firstChoice.push({
                        key: choices[i].FirstChoiceOptionId,
                        value: 1
                    });
                } else {
                    firstChoice[choices[i].FirstChoiceOptionId] = firstChoice[choices[i].FirstChoiceOptionId] + 1;
                }

more if/else..

    function lookup( arr, name ) {
        for(var i = 0, len = arr.length; i < len; i++) {
            if( arr[ i ].key === name )
                return true;
        }
        return false;
    }
1
  • You're using an array where you should be using an object. Commented Mar 25, 2016 at 2:16

3 Answers 3

2

You're using an array where you should be using an object. If you use an object, your code can be rewritten as:

var firstChoice = {};

for (var i = 0; i < choices.length; i++) {
    var firstChoiceOptionId = choices[i].FirstChoiceOptionId;

    if (choices[i].YearTermId == 1) {
        firstChoice[firstChoiceOptionId] = firstChoice[firstChoiceOptionId]
                ? firstChoice[firstChoiceOptionId] + 1
                : 1;

        /* ... */
    }
}

If you need the data as an array afterwards, just map it:

var firstChoiceArray = Object.keys(firstChoice).map(function(key) {
    return {
        key: key,
        value: firstChoice[key]
    };
});

Conversely, if you have an input array and want to convert it to an object for manipulation, reduce it:

var firstChoice = firstChoiceArray.reduce(function(result, current) {
    result[current.key] = current.value;

    return result;
}, {});
Sign up to request clarification or add additional context in comments.

2 Comments

I am trying to keep it as an array key/value so I can plug the data into chart.js after
Just map it to an array after you're done with your manipulation. I'll update my answer.
1

I think you should increment value key, like:

firstChoice[choices[i].FirstChoiceOptionId].value ++;

And I would like to rewrite this code to:

var firstChoice = {};
for (var i = 0; i < choices.length; i++) {
    if (choices[i].YearTermId == 1) {
        if (!firstChoice[choices[i].FirstChoiceOptionId]) {
            firstChoice[choices[i].FirstChoiceOptionId] = 0;
        }
        firstChoice[choices[i].FirstChoiceOptionId]++;
    }
}
console.log(firstChoice);

1 Comment

I want to start the value at 1 - I am trying to plug in the array to chart.js after. When I use the first snippet, .value, cannot read property 'value' of undefined.
0

Try with Array.map: Example:

var a = [{key:"ab","value":1},{key:"cd","value":1},{key:"ef","value":1}];
a.map(function(item){if(item.key == this){item.value++}}, "cd");

So, a[1] will have value 2 after that.

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.