在jQuery中制作多维数组

时间:2019-06-17 16:03:30

标签: php jquery arrays multidimensional-array

我的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中帮助做具有相同结构的相同数组吗?

2 个答案:

答案 0 :(得分:1)

您尝试过吗:

// this is an object that has arrays in it
const object = {
  connection: ['3G', 'wifi'],
  platform: ['mobile', 'desktop', 'tablet']
}

// calling a value:
console.log(object.connection[0]) // expected output: 3G
console.log(object.platform[1]) // expected output: desktop

这不是多维数组(当然是在底层),而是其中包含数组的JavaScript对象。

这也将是一个有效的调用(只是要看它是一个底层的多维数组):

console.log(object['platform'][0]) // expected output: mobile

答案 1 :(得分:1)

基于评论中的讨论,这是答案:

rouge.Character()
from rouge import Character

与您提供的数据集的一个重要区别是它不仅是// creating the object that will hold the valeues let groups = {} // querying DOM for the elements we want const inputList = document.querySelectorAll('input') // iterating over the query result for (let item of inputList) { // get the value of attribute 'data-group' const attribute = item.getAttribute('data-group') // if the attribute is not in the groups object, // then add with an array if (!(attribute in groups)) { groups[attribute] = [] } // push the value of the value attribute to the array groups[attribute].push(item.getAttribute('value')) } // displaying result in the console console.log(groups) // regular syntax console.log('3G from the groups: ', groups.connection[0]) console.log('tablet from the groups: ', groups.platform[2]) // array syntax - multidimensional array console.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)) { groups[key].forEach(item => { console.log(key + ": ", item) }) },而且是<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'>。在HTML5中,将自定义数据添加到DOM元素的方法是使用data前缀,但是您需要将名称附加到属性(我将其命名为 group ,因此它是数据组)。