I've looked into this on other StackOverFlow Questions and I can't seem to find the answer.
I have a form.
<input name="item[1]" class="item" value="1">
<input name="item[2]" class="item" value="50">
<input name="item[3]" class="item" value="12">
I'm trying to get the results of each item input and send them via Ajax to my controller, this is fine when using the form action="" method but via ajax I can't seem to format the data in the array format.
Can anyone tell me what I've done wrong?
$('button#despatchOrder').on('click', function() {
var values = $("input.items");
var myArray = $.map(values, function(value, index) {
return [value];
});
console.log(myArray);
});
Unfortunately Jquery isn't my strongest language but I'm learning. I was expecting the output to be something like.
items = [
1 => 1,
2 => 50,
3 => 12
]
But I have everything associated to the input like
Input 1 = [
accept
accessKey
alt
etc
etc
]
on('click', function() { var values = $("input. items"); var myArray = $. map(values, function(value, index) { return [value]; }); console. log(myArray); });
map() method applies a function to each item in an array or object and maps the results into a new array. Prior to jQuery 1.6, $. map() supports traversing arrays only. As of jQuery 1.6 it also traverses objects.
What are a key and value in an array? Keys are indexes and values are elements of an associative array. Associative arrays are basically objects in JavaScript where indexes are replaced by user-defined keys. They do not have a length property like a normal array and cannot be traversed using a normal for loop.
Can anyone tell me what I've done wrong?
Very little! :-)
$.map
callback, the first argument will be the element, not its value, so you just use its value
property.return [value]
), which you don't want to do; $.map
will build an array from the values you return from each call to the callback.input.items
where you wanted input.item
.You don't need to declare the index
argument if you don't use it.
$('button#despatchOrder').on('click', function() {
var values = $("input.item"); // ***
var myArray = $.map(values, function(element) { // ***
return element.value; // ***
});
console.log(myArray);
});
Example:
$('button#despatchOrder').on('click', function() {
var values = $("input.item");
var myArray = $.map(values, function(element) {
return element.value;
});
console.log(myArray);
});
<input name="item[1]" class="item" value="1">
<input name="item[2]" class="item" value="50">
<input name="item[3]" class="item" value="12">
<button id="despatchOrder">Despatch</button><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
That gives you the items in the array without using the item[1]
and such name. What you've shown that you want
items = [
1 => 1,
2 => 50,
3 => 12
]
isn't valid JavaScript. The above gives you:
myArray = ["1", "50", "12"];
but if you want name/value pairs, probably an array of objects would be best:
myArray = [
{name: "1", value: "1"},
{name: "2", value: "50"},
{name: "3", value: "12"}
]
To do that:
var myArray = $.map(values, function(element) {
return {name: element.name.substring(5, 6), value: element.value};
});
Or of course if you want the whole name item[1]
, just remove the substring
part.
Example:
$('button#despatchOrder').on('click', function() {
var values = $("input.item");
var myArray = $.map(values, function(element) {
return {name: element.name.substring(5, 6), value: element.value};
});
console.log(myArray);
});
<input name="item[1]" class="item" value="1">
<input name="item[2]" class="item" value="50">
<input name="item[3]" class="item" value="12">
<button id="despatchOrder">Despatch</button><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Just for completeness, you can also use the other map
function (jQuery has two), but it doesn't do you any real good here:
var myArray = values.map(function() {
return this.value;
}).get();
Note the .get()
at the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With