8

I'm adding several items to an array using concat in an event-handler as follows:

var selectedValues = [];
$.each($('#selected-levels').data("kendoListBox").dataSource.data(), function(i, item) {
    selectedValues.concat([ item.id ])
});

return {
    "selected" : selectedValues
};

this always returns {level-selected: Array(0)} even though I have checked that there are some items in the dataSource (by stepping through with debugger)

why don't the items appear in the array?

0

2 Answers 2

21

concat doesn't mutate the array, you need to set the value back to selectedValues

selectedValues = selectedValues.concat([ item.id ])

Or use push

selectedValues.push( item.id )
Sign up to request clarification or add additional context in comments.

Comments

1

Ironically, the answer was already hidden in your question, in your question title more exactly.

Actually concat returns the correct new array you want... you just never used the return value !

You have to be aware that the array is not modified in place, but a fresh copy is returned.

So selectedValues.concat([ item.id ]) should be replaced by selectedValues = selectedValues.concat([ item.id ]) if you want to do anything.

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.