0

I want to get format like

[{"1":"6"},{"1":"7"},{"2":"8"}] ) 

But this code shows like this:

[{"cateid":"6"},{"cateid":"7"},{"cateid":"8"}] ) 

How can I replace cateid with integer value?

var seleusrv = [];

$('.catechk:checked').each(function(){
    var cateid = $(this).val();
    var i = 0;

    $('.srvchk:checked').each(function(){
        var srvcateid = $(this).data('cateid');
        var srvid = $(this).val();
        if (cateid == srvcateid) {
            seleusrv.push({cateid: srvid});
        }
    });

    if ($(this).closest('ul').find('.srvchk:checked').length == 0) {
        seleusrv.push({cateid: ''});
    }
});
seleusrv = JSON.stringify(seleusrv);

This is my Html structure:-

<ul>
    <li>
        <input class="catechk collapse_allcheckbox" value="1" type="checkbox"> &nbsp; Air Conditioner
    </li>
    <li>
        <input class="srvchk collapse_checkbox" value="6" data-cateid="1" type="checkbox"> &nbsp; AC Repair
    </li>
    <li>
        <input class="srvchk collapse_checkbox" value="7" data-cateid="1" type="checkbox"> &nbsp; AC Gas Filling
    </li>
</ul>

This is my Architecture

2
  • you have to show us a glimpse of your ul-li HTML. Add it in your question Commented Feb 28, 2018 at 9:51
  • i have edited and added ul-li Commented Feb 28, 2018 at 9:56

2 Answers 2

1

You need to modify your logic like below:-

Check the below working snippet:-

$(document).ready(function(){
  var seleusrv = [];
  $('.catechk').each(function(){
    var obj = $(this);
    var cateid = $(this).val();
    if (obj.closest('ul').find('.srvchk:checked').length == 0) {
        var temp = {};
        temp[cateid] = '';
        seleusrv.push(temp);
    }else{
      obj.closest('ul').find('.srvchk:checked').each(function(){
        var temp = {};
        var catvalue = $(this).val();
        temp[cateid] = catvalue;
        seleusrv.push(temp);
      });
    }
  });
  seleusrv = JSON.stringify(seleusrv);
  console.log(seleusrv);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
    <li>
        <input class="catechk collapse_allcheckbox" value="1" type="checkbox" checked> &nbsp; Air Conditioner
    </li>
    <li>
        <input class="srvchk collapse_checkbox" value="6" data-cateid="1" type="checkbox" checked> &nbsp; AC Repair
    </li>
    <li>
        <input class="srvchk collapse_checkbox" value="7" data-cateid="1" type="checkbox" checked> &nbsp; AC Gas Filling
    </li>
</ul>
<ul>
    <li>
        <input class="catechk collapse_allcheckbox" value="2" type="checkbox" checked> &nbsp; Air Conditioner2
    </li>
    <li>
        <input class="srvchk collapse_checkbox" value="8" data-cateid="1" type="checkbox"> &nbsp; AC Repair2
    </li>
    <li>
        <input class="srvchk collapse_checkbox" value="9" data-cateid="1" type="checkbox"> &nbsp; AC Gas Filling2
    </li>
</ul>

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

4 Comments

I have tries but its give me same variable value as key instead of integer [{"srvcateid":"6"},{"srvcateid":"7"}]
Thanks its works but i need one more confirmation, if Subcategory not present, then it will not get into the loop, so it works ?
@vikasgupta i have modified my code to satisfy both of your requirement.Please check
@vikasgupta glad to help you :):)
0

If you're able to use ES6 in your program, the following should work fine:

seleusrv.push({[cateid]: ''});

See, this section for more details on the specifications. Otherwise, you will have to create a temporary object and then push it:

var temp = {};
temp[cateid] = '';
seleusrv.push(temp);

3 Comments

@vikasgupta -- when you say it doesn't work, you should specify how it doesn't work. Any errors in the console?
I have tried with your second solution its gives me repeated values [{"1":"7"},{"1":"7"}], but i need [{"1":"7"},{"1":"8"}]
@vikasgupta -- that is out of scope of this question. You should check for the existence of the value, and push only if it doesn't exist.

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.