There is a global object that I am trying to fill in on the front by collecting data from the checkbox to be sent north.

 <input class="a" type="checkbox" name="garanty" value="true"> <input class="a" type="checkbox" name="power" value="10kVt"> <input class="a" type="checkbox" name="power" value="5kVt"> 

Js

 var filter_data = { items: [] }; $('.a').change(function(){ if (this.checked == true) { var attr_data = []; filter_data.items.push({ taxonomy: this.name, terms: attr_data, }); attr_data.push(this.value); console.log(filter_data.items); } }); 

The output should be object for php

  $obj = array( array( 'taxonomy' => 'movie_janner', 'terms' => array( 'action', 'comedy' ), ), array( 'taxonomy' => 'actor', 'terms' => array( 103, 115, 206 ), ) ); 

https://jsfiddle.net/qvr75jyu/7/

  • you have just a syntax error there (for a start) - ThisMan
  • @ThisMan - jsfiddle.net/qvr75jyu/7 updated - Nikita Ryazanov
  • You and the code in question update and link (comments are not read) - ThisMan
  • @ThisMan already updated - Nikita Ryazanov
  • so what exactly is the problem? Data is recorded, the list is displayed. What you presented as an answer, firstly to php , secondly, how it does not correspond to the layout that is in question. What is NOT IN and where does it come from? - ThisMan

1 answer 1

 var filterData = { items: [] }; $('.a').change(function(){ if (this.checked == true) { // в js принят camelCase const attrData = []; let filterItem = filterData.items.find(el => el.taxonomy === this.name ); // проверяем, если у нас уже объект с таким именем if(filterItem) { filterItem.terms.push(this.value); } else { filterData.items.push({ taxonomy: this.name, terms: [this.value], }) } console.log(filterData.items); } else { // тут по идее нужно обработать 'выключение' чекбокса, но это если вам нужно } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="a" type="checkbox" name="garanty" value="true"> <input class="a" type="checkbox" name="power" value="10kVt"> <input class="a" type="checkbox" name="power" value="5kVt">