0

I've got the following associative array that I made using Knockout.js.

self.users = ko.observableArray([{name: "Unassigned", value: null}]);

At some point in the application when a button is clicked I have to begin adding more values to this array. However, I'm not quite sure how to do that. Before, I was just using a generic array like so:

$.each(data.users, function(index, user)
{
   vm.users.push(user.first_name + " " + user.last_name);
});

Now that I've got to deal with multiple values I'm not sure how to push both the item and the value to the array. Is there an easy way to do this?

Update:

Following Milimetric's suggestion I updated my code to the following:

vm.users.push({name: user.first_name + " " + user.last_name, value: index + 1});

I then went back and updated my view to the following:

<select id="ticket_assignee" name="ticket[assignee]" data-bind="options: users.name, value: assignee"></select>

Prior to updating the view I was getting the correct number of items in the dropdown. After updating the view I just have a single value which says "d". My guess is that the d comes from the last letter in Unassigned. Did I do something wrong that caused the values to not display correctly?

Pallas
  • 1,499
  • 5
  • 25
  • 57
  • `$.each(data.users, function(index, user) { vm.users.push({name: user.first_name + " " + user.last_name, value: user.value}); });` – Milimetric Mar 25 '15 at 17:38
  • It doesn't look like your array is "associative" at all. It's just an array of objects. – CrimsonChris Mar 25 '15 at 17:50
  • What is the use case for the data structure you are attempting to make. – CrimsonChris Mar 25 '15 at 17:52
  • It's a dropdown list containing a list of user names. When you select the user's name and hit the save button it will take the value associated with that user and post the data. – Pallas Mar 25 '15 at 17:54
  • why not create a function and push its instance . looks readable and cleaner . cheers – super cool Mar 25 '15 at 17:54
  • @Milimetric see updated description – Pallas Mar 25 '15 at 18:02
  • @RandomlyKnighted There is no such thing as an "associative array" in Javascript. What I think you want is an object with dynamic properties. – CrimsonChris Mar 25 '15 at 18:02
  • Maybe not, but can we look past me using the incorrect term please. – Pallas Mar 25 '15 at 18:03
  • @RandomlyKnighted Well, what you have now is just an array that contains objects. If you want something "associative", you should use an object instead of an array. – CrimsonChris Mar 25 '15 at 18:06
  • http://stackoverflow.com/questions/8067590/associative-array-versus-object-in-javascript – CrimsonChris Mar 25 '15 at 18:08
  • It doesn't have to be associative. I just think of associative when I think of key value pairs. I'm just trying to solve this simple problem. – Pallas Mar 25 '15 at 18:10
  • Don't use arrays for key value pairs. – CrimsonChris Mar 25 '15 at 18:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73804/discussion-between-randomlyknighted-and-crimsonchris). – Pallas Mar 25 '15 at 18:14
  • @RandomlyKnighted, I think the best thing for you here is to play with javascript in the console and figure out what the difference between all these concepts is. Terminology can be pedantic but it's also important to understand what an array is. Your problem stems more from a lack of understanding of how programming works and less from a simple mistake. In your updated description, `users.name` is an erroneous attempt to use common sense instead of correct javascript grammar. You should patiently go through the options binding documentation and make sure you are comfortable with it. – Milimetric Mar 25 '15 at 19:54

1 Answers1

0

After talking to CrimsonChris in the chat he concluded than using an object array for what I'm trying to do is overkill. He pointed out that since I'm just trying to create a dropdown list of objects I could use Figure 3 from the Knockout "options" binding documentation as a guide for my current task. After following that example I was able to get everything to work perfectly by using the options, optionsText, and value bindings.

Pallas
  • 1,499
  • 5
  • 25
  • 57