3

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
})
4
  • you can't have a non numeric key for arrays in javascript Commented Jun 26, 2012 at 11:17
  • thank you for the reply, what would be the best approach for this? Commented Jun 26, 2012 at 11:18
  • store what you require as object properties Commented Jun 26, 2012 at 11:19
  • Tried $(this).data(value)? See api.jquery.com/data and api.jquery.com/jQuery.data Commented Jun 26, 2012 at 11:21

2 Answers 2

6

Just use a normal object which works as an associative array anyway:

var myObj = {};
$('#clickme').on('click' , function() {
    myObj["some_text"] = $(this);
});
Sign up to request clarification or add additional context in comments.

2 Comments

sorry to be dumb... but how can i get back the text associated with the object.
var value = myObj["some_text"];
1

This should work:

var myArr = new Array();
myArr["MyKey"] = { name: "myobject" };

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.