Objectives:
- Create Two dimension array in javascript/jquery
- Push data into it
- Loop through each key,value pair
- Call function in loop
Code:
var IDs = [];
/* Find Input elements and push its ID & Value into an array */
$('#divDynamicFields').find("input").each(function () {
IDs.push(this.id, $(this).val());
});
console.log(IDs); /* Now it prints string seprated by ',' */
/* Loop Through Each element in 2D array */
$.each(IDs, function(key, value) {
$.each(key, function(innerKey, innerValue){
CallFunction(id,val);
/* Will This Work ? */
}
}
6 Answers 6
The whole idea is to push to array not two elements, but an array, which consists of two elements:
var IDs = [];
$('#divDynamicFields input').each(function()
{
IDs.push([this.id, $(this).val()]);
});
for (var i = 0; i < IDs.length; i++)
{
CallFunction(IDs[i][0], IDs[i][1]);
}
function CallFunction(id, value)
{
console.log("ID: " + id + ", value: " + value);
}
Comments
You have a couple of problems..
The first one is that you have to add the input as an array
IDs.push([this.id, $(this).val()]);
The second one is that you want to call the ids you just added together, you don't want to do a double loop.
$.each(IDs, function(key, value) {
CallFunction(value[0],value[1]);
});
This is an example:
var IDs = [];
/* Find Input elements and push its ID & Value into an array */
$('#divDynamicFields').find("input").each(function () {
IDs.push([this.id, $(this).val()]);
});
console.log(IDs); /* Now it prints string seprated by ',' */
/* Loop Through Each element in 2D array */
$.each(IDs, function(key, value) {
CallFunction(value[0],value[1]);
});
function CallFunction(id,val) {
console.log(id+","+val);
}
Comments
Use object for insertion
var IDs = {};
$('#divDynamicFields').find("input").each(function () {
IDs[this.id]= $(this).val();
});
And similarly loop
$.each(IDs , function (index, value) {
alert( index + ' : ' + value );
});
6 Comments
news and obj? obj.[this.id] - it should be obj[this.id], shouldn't it? $('#divDynamicFields').find("input") will look a little better with $('#divDynamicFields input').obj.[this.id] to => IDs.[this.id] Is this difficult ?IDs[this.id], not IDs.[this.id].Your push syntax and iteration is wrong. You should do something like:
var IDs = [];
IDs.push([1, 2]);
IDs.push([2, 3]);
/* Loop Through Each element in 2D array */
$.each(IDs, function(key, value) {
alert(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Comments
create two dimensional array
MultiArray = new Array(5)
MultiArray [0] = new Array(2)
MultiArray [0][0] = "Tom"
MultiArray [0][1] = "scientist"
MultiArray [1] = new Array(2)
MultiArray [1][0] = "Beryl"
MultiArray [1][1] = "engineer"
https://trans4mind.com/personal_development/JavaScript/Array2D.htm
Comments
You can try this to create a dimensional array in jquery by using
var IDs = [];
$('#divDynamicFields').find("input").each(function () {
IDs.push({
id: $(this).val()
});
});
console.log(IDs);
This will allow you to send your all data whatever you want to pass in array by ajax.
id,valueas pair