function category_array() {
var send_array = Array();
var send_cnt = 0;
var chkbox = $('input[name="category[]"]:checked');
for(i=0;i<chkbox.length;i ) {
if (chkbox[i].checked == true){
send_array[send_cnt] = chkbox[i].value;
send_cnt ;
}
}
console.log(send_array);
$("#category_list").val(send_array);
}
now result is ['men_a', 'men_b', 'mem_c', 'woman_a', 'woman_d']
What I want {men: [a,b,c], women: [a,d]}
How to do ?
CodePudding user response:
loop over arr
, and get gendre
and its value
and add them to the first res
variable if gendre is male else to second.
const arr = ["men_a", "men_b", "mem_c", "woman_a", "woman_d"];
const res = [[], []];
for (person of arr) {
const [gendre, value] = person.split("_");
if (gendre == "men") res[0].push(value);
else res[1].push(value);
}
console.log(JSON.stringify(res)); // [["a","b"],["c","a","d"]]
Update to store values in an object instead of an array
const arr = ["men_a", "men_b", "mem_c", "woman_a", "woman_d"];
const res = {'men':[],'women':[]};
for (person of arr) {
const [gendre, value] = person.split("_");
if (gendre == "men") res['men'].push(value);
else res['women'].push(value);
}
console.log(JSON.stringify(res)); // {"men":["a","b"],"women":["c","a","d"]}
CodePudding user response:
You need to initialize send_array
as an object, not an array.
Then split the value at the _
character, and use the first part as the object key, and push the second part into the array at that key.
function category_array() {
var send_array = {};
var chkbox = $('input[name="category[]"]:checked');
chkbox.each((i, box) => {
let [gender, item] = box.value.split('_');
if (send_array[gender]) {
send_array[gender].push(item);
} else {
send_array[gender] = [item];
}
});
console.log(send_array);
$("#category_list").val(JSON.stringify(send_array));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Men A <input type="checkbox" name="category[]" value="men_a">
Men B <input type="checkbox" name="category[]" value="men_b">
Men C <input type="checkbox" name="category[]" value="men_c">
Men D <input type="checkbox" name="category[]" value="men_d">
<br>
Women A <input type="checkbox" name="category[]" value="women_a">
Women B <input type="checkbox" name="category[]" value="women_b">
Women C <input type="checkbox" name="category[]" value="women_c">
Women D <input type="checkbox" name="category[]" value="women_d">
<input type="text" id="category_list">
<button type="button" onclick="category_array()">Click</button>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
See comments in the code:
function category_array() {
var send_array = {};
var chkbox = $('input[name="category[]"]:checked');
for(i=0;i<chkbox.length;i ) {
// no need to check that it's checked, you already checked that (in the selector)
const [gender,val] = chkbox[i].value.split("_"); // assuming input is always valid here (it won't be
send_array[gender] = send_array[gender] || [] // make sure it exists
send_array[gender].push(val) // add the new value
}
console.log(send_array);
$("#category_list").val(JSON.stringify(send_array)); // you can't assign an object as a node's value
}