A
A
Andrey Lysov2016-12-06 12:59:49
JavaScript
Andrey Lysov, 2016-12-06 12:59:49

How to create multidimensional array using JS loop?

There is a table (Example):

Cell phone fLY BL6201X
iPhone 5cX
<tr id="tr">
                       <td width="82">Сотовый телефон fLY BL6201<b><input type="hidden" id="eq28" class="name_eq" value="28"></b></td>
                       <td width="140"><i><input type="text" name="sn28" class="sn" value="" placeholder="Серийный номер"></i></td>
                        <td style="width:100px;" align="center"><input type="checkbox" name="garant28"></td><td width="50"><span class="remove_item" style="cursor: pointer; font-weight: bold;color:#ff0000">Х</span></td>
                  </tr>
                  <tr id="tr">
                         <td width="82">iPhone 5c<b><input type="hidden" id="eq5" class="name_eq" value="5"></b></td>
                         <td width="140"><i><input type="text" name="sn5" class="sn" value="" placeholder="Серийный номер"></i></td>
                         <td style="width:100px;" align="center"><input type="checkbox" name="garant5"></td><td width="50"><span class="remove_item" style="cursor: pointer; font-weight: bold;color:#ff0000">Х</span></td>
                 </tr>

Each first td has a Hidden c class "name_eq" whose value is the hardware identifier. next comes class "sn[i]" its value denotes the serial number. I need to make a multidimensional array to send it to the handler. the array should be of the form [[name_eq(value),sn],[name_eq(value)1,sn1],...]
what I found on the Internet returns an error. That's why I'm asking for help from professionals.
I have this code:
var array_eq=[];	//создаю пустой массив
  for (v=0;v<document.getElementsByClassName("name_eq").length;v++){ //перебираю все элементы с классов name_eq
    array_eq[[v]]=document.getElementsByClassName("name_eq")[v].value; //записываю в массив
    
  }

var lenght_ar=parseInt(array_eq.length); // перевожу в число и получаю кол-во элементов

for (var q = 0; q < lenght_ar; q++) {  //дальше все сложно(((

   var num_arr=array_eq[q];
   var name_ar='[name = sn'+num_arr+']';
   array_eq[[[v]]]=$(name_ar).val(); // тут уже только чего не пробовал
   alert($(name_ar).val());

}
console.log(array_eq);

All the best

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Michael, 2016-12-06
@FFxSquall

Try like this:

var array_eq = [],
    nameLength = document.getElementsByClassName("name_eq").length;
for (var i = 0; i < nameLength; i++) {
    var value = document.getElementsByClassName("name_eq")[i].value;
    array_eq.push([value]);
}

for (var i = 0; i < array_eq.length; i++) {
    var num_arr = array_eq[i][0];
    var name_ar = '[name = sn' + num_arr + ']';
    array_eq[i].push($(name_ar).val());

}
console.log(array_eq);

UPD: Fixed bugs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question