Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code where starred_messages is an empty array and starred_id is an empty object.
On the click function I am setting the object's key and value and pushing it in the array.

The first time it is okay but on the next click function instead of adding the new object just to the next index of the array. It is also adding to the previous indexes.

Here's what the array looks like the first time:

0: {msg_92: "<span class="chat-img pull-left"><img src="http://…on glyphicon-time"></span>26 Dec, 6:18 pm</small>"}



Here's what the array looks like the on the second click:

0: {msg_92: "<span class="chat-img pull-left"><img src="http://…on glyphicon-time"></span>26 Dec, 6:18 pm</small>"}, {msg_10: "<span class="chat-img pull-left"><img src="http://…on glyphicon-time"></span>26 Dec, 6:18 pm</small>"}
 1: {msg_92: "<span class="chat-img pull-left"><img src="http://…on glyphicon-time"></span>26 Dec, 6:18 pm</small>"}, {msg_10: "<span class="chat-img pull-left"><img src="http://…on glyphicon-time"></span>26 Dec, 6:18 pm</small>"}


Notice that it added the second object also to the 0th index and the 1st index contains the first object.

I want to add the objects on different indexes.

What I have tried:

var starred_messages = [];
var starred_id = {};

$(document).on('click', '.star', function () {
   var starred_msg_id = $(this).parent().attr('id');
   var starred_msg = $(this).parent().html();
   starred_id[starred_msg_id] = starred_msg;						
   starred_messages.push(starred_id);
});
Posted
Updated 27-Dec-17 1:13am

use function as class[^] object instead of a literal object
if you use literal object[^] the value will get over written every time when the same key is accessed.

refer this example;

function clsMsg(id, msg) {
         this.id = id;
         this.msg = msg;
     }

     var starred_messages = [];
     var starred_id = {};
     var starred_msg_id = 1
     var starred_msg = 'one'
     var item1 = new clsMsg(starred_msg_id, starred_msg);
     starred_messages.push(item1);

     var starred_msg_id = 1
     var starred_msg = 'two'
     var item2 = new clsMsg(starred_msg_id, starred_msg);
     starred_messages.push(item2);

     for (var i = 0; i < starred_messages.length; i++) {
         var item = starred_messages[i];

         console.log('id =' + item.id + ",  message =" + item.msg);
         // 1  one
         // 1 two
     }
 
Share this answer
 
Comments
SL-A-SH 27-Dec-17 6:52am    
But this doesn't set it as key value pair. I want the key to be the id and the value to be msg
Karthik_Mahalingam 27-Dec-17 6:57am    
Yes
It can removed

var starred_messages = [];
Karthik_Mahalingam 27-Dec-17 6:59am    
Id is the key and msg is the message
Replace the function's id-msg with key- value
SL-A-SH 27-Dec-17 7:02am    
Currently it gives me an array like this clsMsg {id: "msg_92", msg: "26 Dec, 6:18 pm"}

But I need it to be {msg_92 : "26 Dec, 6:18 pm"}
Karthik_Mahalingam 27-Dec-17 8:43am    
it is not possible in literal object. you shall use 2D array.
Quote:
The first time it is okay but on the next click function instead of adding the new object just to the next index of the array. It is also adding to the previous indexes.

In fact behind the hood, you push a pointer to object in array.
Next time, you push again a pointer to same object. And since array contain only pointers to 1 object, any change done to the object appear every where in the array.

To avoid this problem, you need to duplicate/copy the object before pushing in array.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900