0

I need to create a array which needs to have custom index and value in jquery each function. For eg. i need to create an array like this [4: 'sampleIndex1','test':'sampleIndex2'];

But while using each function jquery takes '4' index will be an 4th index of an array remaining index 0,1,2,3 will set an value to empty. I dont need 0 1 2 3 index in the array. Instant help is much appreciated!!!

Thanks in advance,

Below are the sample code, I need to create div elements for select options.

<select name="question" id="myselect">
                            <option value="sample1">What is your father's name?</option>
                            <option value="sample2">What is your pets's name?</option>
                            <option value="sample3">What is your school's name?</option>
                        </select> 

$("#myselect").changeselect({width:400}); 
$.fn.changeselect=function(options){
    var defaults = {  
            width: 600 
    };      
    var options = $.extend(defaults, options); 
    var createhtml="";
var selectarr=new Array();
    $("option",this).each(function(k,v){
selectarr[$(this).attr('value')] = $(this).html();  
            createhtml +="<div id='"+k+"'>"+$(this).html()+"</div>";
    });
console.log(selectarr);
$(this).after(createhtml);

};

it shows [] empty array Its need to be like this [{"sample1":"What is your father's name?", "sample2":"What is your pets's name?", "sample3":"What is your school's name?"}]

2
  • Are you sure you don't need an object ? Commented Jul 25, 2012 at 22:38
  • I don't really understand what you are trying to do and are you sure you want to create JSON? Please describe your problem better, provide your code, input and desired output. Commented Jul 25, 2012 at 22:42

3 Answers 3

1

You're looking for a javascript object.

(function () {

  var obj = new Object();

  $('#myselect option').each(function (index, option) {

    var option$ = $(option);

    $('<div/>').attr('id', option$.attr('value')).text(option$.html()).appendTo(/**/);

    obj[option$.attr('value')] = option$.html()

  });

  console.log(obj);

}).call(this)

Here is an example of sorts: http://jsbin.com/irohow/6/edit

Sign up to request clarification or add additional context in comments.

Comments

0

That's not a valid javascript array. I think you want something more like:

[{4: 'sampleIndex1'}, {'test':'sampleIndex2'}]

1 Comment

yes... But what i need is to have different keys for this array.
0

You can't have string keys in an array - you should use an object for this -

var a = new Object();
a['4'] = "Some Value";
a['5'] = "Another Value";

This will give you -

{"4":"Some Value","5":"Another Value"}

1 Comment

And if you need to iterate over the keys, use something like Object.keys or underscore.js

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.