16

This is what I have so far and the shoe types are boots, wellingtons, leather, trainers (in that order)

I want to iterate through and assign the value so I haves something like

var shoeArray = { boots : '3', wellingtons: '0', leather : '1', trainers: '3'};

at the moment I just get an array of {3,0,1,3} which I can work with but it is not very helpful.

function shoe_types() {
    var shoeArray = [];
    $('[type=number]').each(function(){
        $('span[data-field='+$(this).attr('id')+']').text($(this).val());      
        shoeArray.push ( parseInt($(this).val()) );      
    });             
    return shoeArray;        
}
4
  • 7
    There are no "associative arrays" in JS. You're talking about objects. Some reading material: w3schools.com/js/js_objects.asp Commented Nov 5, 2013 at 11:26
  • the shoe types are fixed - there is only and will only be the 4 types Commented Nov 5, 2013 at 11:27
  • 1
    @Roscoeh i think $(this).attr('id') has shoe types Commented Nov 5, 2013 at 11:27
  • 2
    An array in JavaScript would be represented as [3,0,1,3], not {3,0,1,3}. Commented Nov 5, 2013 at 11:31

5 Answers 5

29

Check this function

function shoe_types() {
    var shoeArray = {}; // note this
    $('[type=number]').each(function(){
       $('span[data-field='+$(this).attr('id')+']').text($(this).val());
       shoeArray[$(this).attr('id')] =  parseInt($(this).val()) ;
    });
    return shoeArray;

}

PS: Assuming $(this).attr('id') has all the shoe types

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

Comments

11

Associative array in javascript is the same as object

Example:

var a = {};
a["name"] = 12;
a["description"] = "description parameter";
console.log(a); // Object {name: 12, description: "description parameter"}

var b = [];
b["name"] = 12;
b["description"] = "description parameter";
console.log(b); // [name: 12, description: "description parameter"]

2 Comments

Did you mean to use curly brackets for var b?
i don't think so @Mark
7

What you want is a function that will return an object {}

LIVE DEMO

function shoe_types(){
   var shoeObj = {};
   $('[name="number"]').each(function(){
     shoeObj[this.id] = this.value;
   });
   return shoeObj;
}

shoe_types(); // [object Object]

Comments

2

You can try this to create an associate array in jquery

var arr = {};
$('[type=number]').each(function(){
    arr.push({
         $(this).attr('id'): $(this).val()              
     });
});

console.log(arr);

This will allow you to send your all data whatever you want to pass in array by ajax.

Comments

0

if $(this).attr('id') is the type of shoes you can try that

shoeArray[$(this).attr('id')] = parseInt($(this).val());

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.