Adding key/value into array where value is a object
I want to add some text as a key and Object as a value.
Example
$('#clickme').on('click' , function() {
push to array => "some_text" as (value) and $(this) as key
})
Adding key/value into array where value is a object
I want to add some text as a key and Object as a value.
Example
$('#clickme').on('click' , function() {
push to array => "some_text" as (value) and $(this) as key
})
Share
Improve this question
edited Jun 26, 2012 at 11:17
Sirko
74.1k19 gold badges155 silver badges190 bronze badges
asked Jun 26, 2012 at 11:16
user1184100user1184100
6,90430 gold badges84 silver badges122 bronze badges
4
- you can't have a non numeric key for arrays in javascript – Mukesh Soni Commented Jun 26, 2012 at 11:17
- thank you for the reply, what would be the best approach for this? – user1184100 Commented Jun 26, 2012 at 11:18
- store what you require as object properties – Mukesh Soni Commented Jun 26, 2012 at 11:19
-
Tried
$(this).data(value)
? See api.jquery./data and api.jquery./jQuery.data – Stefan Commented Jun 26, 2012 at 11:21
2 Answers
Reset to default 6Just use a normal object which works as an associative array anyway:
var myObj = {};
$('#clickme').on('click' , function() {
myObj["some_text"] = $(this);
});
This should work:
var myArr = new Array();
myArr["MyKey"] = { name: "myobject" };
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745439444a4627763.html
评论列表(0条)