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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

此矩陣函數(shù)返回輸入“'1 2 3\n4 5 6”的列的確切步驟是什么?

此矩陣函數(shù)返回輸入“'1 2 3\n4 5 6”的列的確切步驟是什么?

回首憶惘然 2022-01-01 20:10:30
這個(gè)矩陣函數(shù)返回輸入列的確切步驟是什么'1 2 3\n4 5 6'?function Matrix(input) {  const thisRows =    input.split('\n').map(row => row.split(' ').map(Number));  return {    rows: thisRows,    columns: thisRows[0].map((col,i) => thisRows.map(row => row[i]))  }}我不明白如何thisRows[0].map((col,i) => thisRows.map(row => row[i]))正確返回列,更具體地說(shuō),為什么我們使用thisRows[0].
查看完整描述

2 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

此函數(shù)的輸入將是一個(gè)字符串,例如


'1 2 3\n4 5 6'

因此,該函數(shù)在 \n 上拆分,返回


['1 2 3', '4 5 6']

然后,該函數(shù)循環(huán)遍歷數(shù)組中的每個(gè)項(xiàng)目,并在每個(gè)項(xiàng)目上拆分項(xiàng)目' '并將每個(gè)子項(xiàng)目轉(zhuǎn)換為一個(gè)數(shù)字,返回


[[1, 2, 3], [4, 5, 6]]

該函數(shù)然后抓取第一行以獲取矩陣的寬度。該行未被使用,但循環(huán)內(nèi)的索引被使用。然后,在循環(huán)內(nèi)部,循環(huán)遍歷每一行并獲取指定索引處的元素,返回列而不是行。


例如。在第一個(gè)循環(huán)中,索引為 0。在第二個(gè)循環(huán)中,我們將遍歷每一行并獲取索引為 0 的元素:


i = 0

// first iteration

[1, 2, 3][i] = 1

// second iteration

[4, 5, 6][i] = 4


=> [1, 4]

然后,下一個(gè)索引將是 1,所以同樣的事情發(fā)生:


i = 1

// first iteration

[1, 2, 3][i] = 2

// second iteration

[4, 5, 6][i] = 5


=> [2, 5]

等等。然后,一旦兩個(gè)循環(huán)都完成,所有列都將在數(shù)組中返回:


[[1, 4], [2, 5], [3, 6]]

這是一個(gè)例子,有一些 console.logs 來(lái)說(shuō)明我的意思:


function Matrix(input) {

  const thisRows =

    input.split('\n').map(row => row.split(' ').map(Number));


  return {

    rows: thisRows,

    columns: thisRows[0].map((col,i) => {

      console.log('index:', i)

      return thisRows.map(row => {

        console.log('row:', JSON.stringify(row), '\nel:', row[i])

        return row[i]

      })

    })

  }

}


console.log(Matrix('1 2 3\n4 5 6'))


查看完整回答
反對(duì) 回復(fù) 2022-01-01
?
MMMHUHU

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


const thisRows =

    input.split('\n').map(row => row.split(' ').map(Number));

你有行: [ [1,2,3], [4,5,6] ]


現(xiàn)在要獲取列,您想遍歷第一行并使用 map 函數(shù)中當(dāng)前值的索引來(lái)獲取另一個(gè) map 中每一行的值:


i=0 [1,4],

i=1 [2,5],

i=2 [3,6]


查看完整回答
反對(duì) 回復(fù) 2022-01-01
  • 2 回答
  • 0 關(guān)注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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