简体   繁体   English

如何根据 Javascript 中复选框的名称和值创建数组?

[英]How can I create an array from names and values of checkboxes in Javascript?

Imagine I have an array of selected checkboxes.想象一下,我有一组选定的复选框。 Each has a value and name that corresponds with a filter.每个都有一个与过滤器对应的值和名称。

<input class="filter-item--checkbox" type="checkbox" name="colors" value="blue">
<input class="filter-item--checkbox" type="checkbox" name="colors" value="orange">
<input class="filter-item--checkbox" type="checkbox" name="items" value="chair">
<input class="filter-item--checkbox" type="checkbox" name="items" value="bed">
<input class="filter-item--checkbox" type="checkbox" name="material" value="plastic">
<input class="filter-item--checkbox" type="checkbox" name="material" value="wood">

How can I sort these into one array, like below?如何将这些排序为一个数组,如下所示?

const filters = [
 'colors': [ 'blue', 'orange'],
 'items': ['chair', 'bed'],
 'material': ['plastic', 'wood'],
]

You could get the elements and build an object of arrays.您可以获取元素并构建数组对象。

 const result = {}; for (const { name, value } of document.getElementsByClassName('filter-item--checkbox')) { (result[name] ??= []).push(value); } console.log(result);
 <input class="filter-item--checkbox" type="checkbox" name="colors" value="blue"> <input class="filter-item--checkbox" type="checkbox" name="colors" value="orange"> <input class="filter-item--checkbox" type="checkbox" name="items" value="chair"> <input class="filter-item--checkbox" type="checkbox" name="items" value="bed"> <input class="filter-item--checkbox" type="checkbox" name="material" value="plastic"> <input class="filter-item--checkbox" type="checkbox" name="material" value="wood">

For the OP's use case, where it was said that a solution should target just checked checkbox controls one could/should utilize the FormData Web-API and its entries method together with a reduce task.对于 OP 的用例,据说解决方案应该针对刚刚选中的复选框控件,可以/应该将FormData Web-API及其entries方法与reduce任务结合使用。 Passing a form reference directly into the FormData constructor ensures only active control data to be part of the form data object.将表单引用直接传递到FormData构造函数可确保只有活动控件数据成为表单数据对象的一部分。 Thus the name/key and value of an unchecked checkbox control gets not reflected by the form data object.因此,表单数据对象不会反映未选中复选框控件的名称/键和值。

 const formData = new FormData(document.forms[0]); console.log( Array .from( formData.entries() ) .reduce((result, [key, value]) => { const groupedValueList = (result[key] ??= []); groupedValueList.push(value); return result; }, {}) );
 .as-console-wrapper { min-height: 85%!important; }
 <form> <input type="checkbox" name="colors" value="blue" checked/> <input type="checkbox" name="colors" value="orange" checked/> <input type="checkbox" name="items" value="chair" checked/> <input type="checkbox" name="items" value="bed" checked/> <input type="checkbox" name="material" value="plastic" checked/> <input type="checkbox" name="material" value="wood" checked/> </form>

In case of collecting all grouped checkbox values regardless of their checked state one just needs to change the above approach slightly too ...如果收集所有分组的复选框值而不管它们的checked状态如何,只需要稍微改变上述方法......

 console.log( Array .from( document.querySelectorAll('form [type="checkbox"]') ) .reduce((result, { name:key, value }) => { const groupedValueList = (result[key] ??= []); groupedValueList.push(value); return result; }, {}) );
 .as-console-wrapper { min-height: 85%!important; }
 <form> <input type="checkbox" name="colors" value="blue" checked/> <input type="checkbox" name="colors" value="orange"/> <input type="checkbox" name="items" value="chair" checked/> <input type="checkbox" name="items" value="bed"/> <input type="checkbox" name="material" value="plastic" checked/> <input type="checkbox" name="material" value="wood"/> </form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过获取复选框名称和值来创建JSON对象? - How can I create a JSON object by getting checkboxes names and values? 如何从javascript中的数组值创建对象键? - How can i create an object keys from array values in javascript? 如何动态创建数组元素并将值(来自具有相同名称的变量)分配给元素 - How can I create array elements dynamically and assign values (from variable with the same names) to the elements javascript如何使用键和复选框值创建多维数组 - javascript How to create multidimensional array with key and checkboxes values 如何将由 for 循环制作的复选框中选中的数组值推送到另一个数组? - How can I push array values checked in checkboxes made from a for loop to another array? 如何使用Redux从一系列复选框中获取值以与其他一些组件共享? - How can I grab values to share with some other components from an array of checkboxes using Redux? 如何使用jQuery或JavaScript从一组选中的复选框中获取类名数组? - How to get an array of class-names from a set of checked checkboxes using jQuery or JavaScript? jQuery-如何从元素值创建数组? - jquery - How can I create an array from element values? 如何将Ajax响应中的值附加到JavaScript中的数组? - How can I append values from an Ajax response to an array in JavaScript? 如何从对象数组创建 JavaScript object? - How can I create a JavaScript object from an array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM