在 jquery 中创建一个多维数组

我的 html 代码是:


    <input class='change_connection' name='test[connection][]' type='checkbox' value="3G">

    <input class='change_connection' name='test[connection][]' type='checkbox' value="wifi">

    <input class='change_platform' name='test[platform][]' value='mobile' type='checkbox'>

    <input class='change_platform' name='test[platform][]' value='desktop' type='checkbox'>

    <input class='change_platform' name='test[platform][]' value='tablet' type='checkbox'>

在 php 中,我用它创建了一个多维数组,如下所示:


    Array

(

    [connection] => Array

        (

            [0] => 3G

            [1] => wifi

        )


    [platform] => Array

        (

            [0] => mobile

            [1] => desktop

            [2] => tablet

        )


)

那么你能帮忙在jquery中用相同的结构做相同的数组吗?


森林海
浏览 401回答 2
2回答

慕勒3428872

根据评论中的讨论,以下是答案:// creating the object that will hold the valeueslet groups = {}// querying DOM for the elements we wantconst inputList = document.querySelectorAll('input')// iterating over the query resultfor (let item of inputList) {&nbsp; // get the value of attribute 'data-group'&nbsp; const attribute = item.getAttribute('data-group')&nbsp; // if the attribute is not in the groups object,&nbsp; // then add with an array&nbsp; if (!(attribute in groups)) {&nbsp; &nbsp; groups[attribute] = []&nbsp; }&nbsp; // push the value of the value attribute to the array&nbsp; groups[attribute].push(item.getAttribute('value'))}// displaying result in the consoleconsole.log(groups)// regular syntaxconsole.log('3G from the groups: ', groups.connection[0])console.log('tablet from the groups: ', groups.platform[2])// array syntax - multidimensional arrayconsole.log('3G from the groups (array): ', groups['connection'][0])console.log('tablet from the groups (array): ', groups['platform'][2])// if the keys in the groups object are not known// (so you cannot count on calling them by a string),// then this is how you iterate through the object:for (let key of Object.keys(groups)) {&nbsp; groups[key].forEach(item => {&nbsp; &nbsp; console.log(key + ": ", item)&nbsp; })}<input class='change_connection' name='test[connection][]' type='checkbox' data-group="connection" value="3G"><input class='change_connection' name='test[connection][]' type='checkbox' data-group="connection" value="wifi"><input class='change_platform' name='test[platform][]' data-group="platform" value='mobile' type='checkbox'><input class='change_platform' name='test[platform][]' data-group="platform" value='desktop' type='checkbox'><input class='change_platform' name='test[platform][]' data-group="platform" value='tablet' type='checkbox'>从您提供的数据集的一个重要区别是,它不仅是data不过data-group。在 HTML5 中,向 DOM 元素添加自定义数据的方法是使用data-*前缀,但随后您需要将您的名称附加到属性(我将其命名为group,因此它在 HTML 中是data-group)。

守着星空守着你

你有没有试过这个:// this is an object that has arrays in itconst object = {&nbsp; connection: ['3G', 'wifi'],&nbsp; platform: ['mobile', 'desktop', 'tablet']}// calling a value:console.log(object.connection[0]) // expected output: 3Gconsole.log(object.platform[1]) // expected output: desktop这不是一个多维数组(当然,它在底层是),而是一个包含数组的 JavaScript 对象。这也是一个有效的调用(只是为了看到它是一个多维数组):console.log(object['platform'][0]) // expected output: mobile
打开App,查看更多内容
随时随地看视频慕课网APP