0

My Jquery code

      var attrid        = [];
      var attrvalue     = [];

      $.each($(".attname1"), function(index){      

       if($('.getval'+index+'').val() != undefined){

          attrid.push($('.getval'+index+'').attr("id"));
          attrvalue.push($('.getval'+index+'').val());                  
        }

     });

    //outcome data
    ["5931", "5950", "5951", "5952"]  //id

    ["cas", "bsd", "Canvas", "Grey"] //name

The result format that I actually want:

$ary = array (
              [0] => array(
                      Id => 5931,
                      value=> cas,
                     ),
              [1] => array(
                      Id => 5950,
                      value=> bsd,
                     ),
              [2] => array(
                      Id => 5951,
                      value=> Canvas,
                     ),
              [3] => array(
                      Id => 5952,
                      value=> Grey,
                     )
       );

Question:

Above code are using Jquery to loop the data and store into array, I'm trying to create loop to create the result format the I want, but I failed to do that,anyone can provide me some sample code or idea which is can using Jquery to create the results format that I provided.Thank you.

0

1 Answer 1

1

Make one array and push objects onto it:

var ary = [];
$('.attname1').each(function(index) {
    var getval = $('.getval' + index);
    if (getval.val()) {
        ary.push({
            id: getval.attr('id'),
            value: getval.val()
        });
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for you answer :)

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.