第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 jquery 中創(chuàng)建一個多維數(shù)組

在 jquery 中創(chuàng)建一個多維數(shù)組

PHP
森林海 2021-08-27 09:31:17
我的 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 中,我用它創(chuàng)建了一個多維數(shù)組,如下所示:    Array(    [connection] => Array        (            [0] => 3G            [1] => wifi        )    [platform] => Array        (            [0] => mobile            [1] => desktop            [2] => tablet        ))那么你能幫忙在jquery中用相同的結(jié)構(gòu)做相同的數(shù)組嗎?
查看完整描述

2 回答

?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗 獲得超6個贊

根據(jù)評論中的討論,以下是答案:


// 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'>

從您提供的數(shù)據(jù)集的一個重要區(qū)別是,它不僅是data不過data-group。在 HTML5 中,向 DOM 元素添加自定義數(shù)據(jù)的方法是使用data-*前綴,但隨后您需要將您的名稱附加到屬性(我將其命名為group,因此它在 HTML 中是data-group)。


查看完整回答
反對 回復(fù) 2021-08-27
?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗 獲得超8個贊

你有沒有試過這個:


// 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

這不是一個多維數(shù)組(當(dāng)然,它在底層是),而是一個包含數(shù)組的 JavaScript 對象。


這也是一個有效的調(diào)用(只是為了看到它是一個多維數(shù)組):


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


查看完整回答
反對 回復(fù) 2021-08-27
  • 2 回答
  • 0 關(guān)注
  • 434 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號