4

I have a function which gets the data from various input ( multiple values ) and populate into a table.

Here is the jquery snippet :

var row =1   
$("input[name='product_id[]']").each(function() {               
        $rows[row].cells[0].innerText = $(this).val();      
        row = row+1;
    });

    row=1;
    $("input[name='product_name[]']").each(function() {             
        $rows[row].cells[1].innerText = $(this).val();      
        row = row+1;
    });

    row=1;
    $("input[name='manufacturer[]']").each(function() {             
        $rows[row].cells[2].innerText = $(this).val();      
        row = row+1;
    });

    row=1;
    $("input[name='composition[]']").each(function() {              
        $rows[row].cells[3].innerText = $(this).val();      
        row = row+1;
    });

I was wondering if I can combine multiple iterators into a single iterator ?

Thanks Kiran

4 Answers 4

4

You can join the selectors by commas:

$("input[name='product_id[]'], input[name='product_name[]'], input[name='manufacturer[]'], input[name='composition[]']")
.each(function() {
  // ...
});

To be more DRY, use an array:

const selectorStr = ['product_id', 'product_name', 'manufacturer', 'composition']
  .map(str => `input[name='${str}[]']`)
  .join(',');
$(selectorStr).each(function() {
  // ...
});

If you need row to be 1 in all but the first iteration, then:

['product_id', 'product_name', 'manufacturer', 'composition']
  .forEach((str, i) => {
    if (i !== 0) {
      row = 1;
    }
    $(`input[name='${str}[]']`).each(function(){
      // etc
    });
  });
Sign up to request clarification or add additional context in comments.

3 Comments

Good one. But the variable row value will differ. It should be initialized to 1, before each separate iteration starts, as he added in question. In your case the common row value will be shared in all iterations
OP didn't describe his use of rows or show how / if it's relevant to the iterations, but if needed, use forEach and check the current index
Yes he didn't mentioned, but he has initialized row to 1 before each iteration. But now your updated answer for row logic looks perfect :)
4

Create a common function, this will help your row logic, which gets value 1 before each iteration

function iteratorOperation(){
}

And then pass this to the iterators,

$("input[name='product_id[]']").each(iteratorOperation);

row=1;
$("input[name='product_name[]']").each(iteratorOperation);

row=1;
$("input[name='manufacturer[]']").each(iteratorOperation);

row=1;
$("input[name='composition[]']").each(iteratorOperation);

Comments

2

Use , to separate multiple selectors

$("input[name='product_id[]'],input[name='product_name[]'],input[name='manufacturer[],input[name='composition[]']").each(function() {               

});

Comments

2

Perhaps this is interesting?

note the escaping of the []

const fieldNames = ["product_id[]", "product_name[]", "manufacturer[]", "composition[]"];
const selector = fieldNames.map(item => `input[name='${item}\\\\[\\\\]']`).join(", ")
$rows.each((row) => { 
  $(selector).each(() => {
    let idx = fieldNames.indexOf(this.name)
    row.cells[idx].innerText = $(this).val(); // jquery object or collection?
  })
})

Comments

Your Answer

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.