0

Objectives:

  1. Create Two dimension array in javascript/jquery
  2. Push data into it
  3. Loop through each key,value pair
  4. 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 ? */
 }
 }
asked Oct 1, 2014 at 11:45
5
  • 2
    I don't see 2 dimensions in that 1 dimensional array. So I guess the answer is nope, that won't work. Commented Oct 1, 2014 at 11:50
  • @Spokey i want to create 2d array that holds id,value as pair Commented Oct 1, 2014 at 11:52
  • 1
    Take a look at fiddle. Commented Oct 1, 2014 at 11:56
  • @Regent Post it as answer :) Commented Oct 1, 2014 at 12:00
  • @Shaggy ok, as it looks good for you :) Commented Oct 1, 2014 at 12:03

6 Answers 6

11

The whole idea is to push to array not two elements, but an array, which consists of two elements:

JSFiddle.

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);
}
Pratik Joshi
11.7k7 gold badges44 silver badges73 bronze badges
answered Oct 1, 2014 at 12:02
Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

JSFiddle

answered Oct 1, 2014 at 12:05

Comments

1

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 );
});
answered Oct 1, 2014 at 11:56

6 Comments

What is 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').
@Regent , its typo error bro , logic is important.Thanks corrcted answer.DOWNVOTER care to comment? Answer is correct?
A human being can understand the typo , obj.[this.id] to => IDs.[this.id] Is this difficult ?
Is it difficult to correct all typos and mistakes in the answer? Answer should be answer, not "find and fix all mistakes and typos in it". I do not downvote answers, because I always hope that people will correct them. And downvoting answer as vengeance is very strange, don't you think so? And once more: it has to be IDs[this.id], not IDs.[this.id].
Ok you can do +1 if answer is correct . Now its correct ,and good for future users.
|
0

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>

answered Oct 1, 2014 at 12:00

Comments

0

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

sauerburger
5,1884 gold badges35 silver badges46 bronze badges
answered Aug 23, 2017 at 12:01

Comments

0

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.

answered Sep 1, 2017 at 10:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.