0

I can't really find a good match for this question,

I have this task of getting the values from this elements

Card record 1 :
<input type="text" name="card[][company]" />
<input type="text" name="card[][bank]" />
<input type="text" name="card[][hospital]" />

Card record 2 :
<input type="text" name="card[][company]" />
<input type="text" name="card[][bank]" />
<input type="text" name="card[][hospital]" />

I want to get their values to be like this so I can pass it on to my controller via AJAX (CI)

[0] 
    card[0][company] = ABC
    card[1][bank] = DEF
    card[2][hospital] = GHI
[1] 
    card[0][company] = ABC
    card[1][bank] = DEF
    card[2][hospital] = GHI

I have tried $.each with .push and .map but can't really figure out the correct manipulation, JQUERY answers please.

1
  • can't you just use serialize() on the form and let jQuery do all the heavy lifting internally? Commented Oct 11, 2014 at 14:02

2 Answers 2

1

Well, you can do this

var arr = $('input[name^=card]').map(function(){
   return { this.name : this.value }
}).get(), newArr = [];
while(arr.length > 0)
  newArr.push(arr.splice(0 , 3));

newArr will contain what you want.

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

Comments

0

In JavaScript, there is no concept of multidimensional array. But you can use it same functionality with array of array. Like while declaring Var are=[[1,2,3],[4,5,6]] Here you can access array elements as follows

Var a=are[0] returns [1,2,3] Var b =a[0] returns 1 So if you want to push some data you need to push in initial array which is array of array. And here initial array can be used for any array manipulation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.