javascript - Adding keyvalue into array where value is a object - Stack Overflow

Adding keyvalue into array where value is a objectI want to add some text as a key and Object as a val

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
Add a ment  | 

2 Answers 2

Reset to default 6

Just 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信