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

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

如何動(dòng)態(tài)訪問數(shù)組中的索引?

如何動(dòng)態(tài)訪問數(shù)組中的索引?

胡子哥哥 2021-06-12 02:09:27
我有一個(gè)帶有一些嵌套數(shù)組的 rootArray,它可以是任何深度。我想動(dòng)態(tài)訪問由索引列表定義的某個(gè)內(nèi)部數(shù)組,以將新內(nèi)容推送到那里。例如,如果索引列表是currentFocusedArray = [1,2]我想要...rootArray[1][2].push('new content')動(dòng)態(tài)的,而不是硬連線的。也許這是樹木不讓我看到森林的情況,或者我在死胡同里。我只是做一個(gè)簡(jiǎn)單的筆記應(yīng)用程序,帶有反應(yīng),非常簡(jiǎn)單。https://codesandbox.io/embed/quirky-gauss-09i1h歡迎任何建議!希望我沒有浪費(fèi)你的時(shí)間。提前致謝。
查看完整描述

3 回答

?
收到一只叮咚

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

您可以編寫 find 數(shù)組以根據(jù)您的焦點(diǎn)數(shù)組獲取數(shù)組。使用數(shù)組方法reduce從索引數(shù)組中查找節(jié)點(diǎn)


var updateNode = focus.reduce((node,index) => node && node[index], notes);

updateNode && updateNode.push("new content");


查看完整回答
反對(duì) 回復(fù) 2021-06-18
?
largeQ

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

您可以使用 for 循環(huán)來實(shí)現(xiàn)。


let myArray = rootArray

for(let i = 0; i < currentFocusedArray.length; i++){

    myArray = myArray[currentFocusedArray[i]]

}

在此之后,您將myArray引用 的深層嵌套值rootArray。


let rootArray = {

    "1": {

        "2": []

    }

}

 

let currentFocusedArray = [1, 2]

 

let myArray = rootArray

for(let i = 0; i < currentFocusedArray.length; i++){

    myArray = myArray[currentFocusedArray[i]]

}


myArray.push("new content")


console.log(myArray)


查看完整回答
反對(duì) 回復(fù) 2021-06-18
?
PIPIONE

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

您可以使用reduce創(chuàng)建這樣的函數(shù)來在任何級(jí)別設(shè)置嵌套數(shù)組元素。


const rootArray = []


function set(arr, index, value) {

  index.reduce((r, e, i, a) => {

    if (!r[e]) {

      if (a[i + 1]) r[e] = []

    } else if (!Array.isArray(r[e])) {

      if (a[i + 1]) {

        r[e] = [r[e]]

      }

    }


    if (!a[i + 1]) {

      if (Array.isArray(r)) {

        r[e] = value

      }

    }


    return r[e]

  }, arr)

}


set(rootArray, [1, 2], 'foo');

set(rootArray, [1, 1, 2], 'bar');

set(rootArray, [1, 2, 2], 'baz');



console.log(JSON.stringify(rootArray))


查看完整回答
反對(duì) 回復(fù) 2021-06-18
  • 3 回答
  • 0 關(guān)注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報(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)