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

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

是否可以通過指定索引數(shù)組來訪問數(shù)組的第 n 個深度元素?

是否可以通過指定索引數(shù)組來訪問數(shù)組的第 n 個深度元素?

MMTTMM 2022-08-27 09:25:27
更新謝謝大家的投入,感謝你們,我現(xiàn)在意識到我對JS做了一個嚴(yán)重錯誤的假設(shè)。實(shí)際上,如果修改它,下面提供的代碼可以正常工作:...  let currentPlaceInArray;  for (let i = 0; i < position.length; i++) {    let place = position[i];    if (i + 1 === position.length) return currentPlaceInArray[place] = content; // Added this line now    /***Explanation:    Without this added line, the currentPlaceInArray variable is reassigned in the end     to 'needle' in the present example. Thus, assigning it to something else (like    currentPlaceInArray = ['updated']) will not have any effect on the array, because    currentPlaceInArray is not assigned to it anymore, it's assigned to the string 'needle'    ***/    currentPlaceInArray = currentPlaceInArray ? currentPlaceInArray[place] : myArray[place];   }原始問題我有一個大型非結(jié)構(gòu)化多維數(shù)組,我不知道它有多少嵌套數(shù)組,它們具有不同的長度,就像這個簡化的例子一樣:var myArray =[  [    [],[]  ],  [    [],[],[],[]  ],  [    [],    [      [        ['needle']      ]    ]  ]];我希望能夠更新到其他內(nèi)容。這可以通過執(zhí)行'needle'myArray[2][1][0][0] = 'updated';我想創(chuàng)建一個函數(shù),它只接受2條信息作為參數(shù):1)要編寫的字符串和2)一個數(shù)組,其中包含要更新的數(shù)組項(xiàng)的位置。在上面的情況下,它將被稱為:。myArraychangeNeedle('updated', [2, 1, 0, 0])但是,我不能將變量分配給數(shù)組鍵,只能分配給其值。如果可以將變量分配給數(shù)組鍵,我可以用當(dāng)前位置更新該變量(即,var currentPosition將是myArray[x],而currentPosition[y]將是myArray[x][y]) [更新:它是完全相反的,將變量分配給數(shù)組將精確指向該數(shù)組,所以]。這樣:currentPosition[y] === myArray[x][y]function changeNeedle(content, position) {  /***  content: a string  position: an array, with the position of the item in myArray to be updated  ***/  let currentPlaceInArray;  for (let i = 0; i < position.length; i++) {    let place = position[i];    currentPlaceInArray = currentPlaceInArray ? currentPlaceInArray[place] : myArray[place];   }  currentPlaceInArray = content;}是否可以在不使用 的情況下實(shí)現(xiàn)此函數(shù), 窗口.Function() 還是將原型添加到數(shù)組中?changeNeedleeval
查看完整描述

4 回答

?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個贊

起初,Chase的答案似乎是正確的(使用遞歸),但似乎您希望您的代碼僅遵循提供給函數(shù)的路徑。


如果您確定要更改的元素的位置,您仍然可以使用遞歸方法:


純 JS 遞歸解決方案

var myArray = [

  [

    [],

    []

  ],

  [

    [],

    [],

    [],

    []

  ],

  [

    [],

    [

      [

        ['needle']

      ]

    ]

  ]

];


function changeNeedle(arr, content, position) {


  // removes first element of position and assign to curPos

  let curPos = position.shift();


  // optional: throw error if position has nothing

  // it will throw an undefined error anyway, so it might be a good idea

  if (!arr[curPos])

    throw new Error("Nothing in specified position");


  if (!position.length) {

    // finished iterating through positions, so populate it

    arr[curPos] = content;

  } else {

    // passes the new level array and remaining position steps

    changeNeedle(arr[curPos], content, position);

  }


}


// alters the specified position

changeNeedle(myArray, 'I just got changed', [2, 1, 0, 0]);


console.log(myArray);


但是,如果要搜索與內(nèi)容對應(yīng)的元素,仍必須循環(huán)訪問每個元素。

查看完整回答
反對 回復(fù) 2022-08-27
?
largeQ

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

如果要避免導(dǎo)入庫,則具有就地更新的特定于陣列的解決方案將如下所示:


function changeNeedle(searchValue, replaceWith, inputArray=myArray) {

  inputArray.forEach((v, idx) => {

    if (v === searchValue) {

      inputArray[idx] = replaceWith;

    } else if (Array.isArray(v)) {

      changeNeedle(searchValue, replaceWith, v);

    }

  });

  return inputArray;

}

然后,您可以調(diào)用,并且您的值將被更改以將 的實(shí)例更改為 。changeNeedle('needle', 'pickle', myArray);myArrayneedlepickle


返回值,然后如下所示:myArray


JSON.stringify(myArray, null, 4);

[

    [

        [],

        []

    ],

    [

        [],

        [],

        [],

        []

    ],

    [

        [],

        [

            [

                [

                    "pickle"

                ]

            ]

        ]

    ]

]

更新:根據(jù)您的回復(fù),遵循已知更新的確切路徑的非遞歸解決方案。


function changeNeedle(newValue, atPositions = [0], mutateArray = myArray) {

  let target = mutateArray;

  atPositions.forEach((targetPos, idx) => {

    if (idx >= atPositions.length - 1) {

      target[targetPos] = newValue;

    } else {

        target = target[targetPos];

    }

  });

  return mutateArray;

}

請參見:https://jsfiddle.net/y0365dng/


查看完整回答
反對 回復(fù) 2022-08-27
?
蕪湖不蕪

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個贊

我建議考慮經(jīng)過充分測試的Lodash函數(shù)更新(或設(shè)置,等等)。我認(rèn)為這非常接近你所需要的。


查看完整回答
反對 回復(fù) 2022-08-27
?
慕妹3146593

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

不,你不能。


執(zhí)行此操作的最簡單方法是存儲要修改的數(shù)組 () 和鍵 ()。然后,您只需要進(jìn)行常規(guī)分配,例如.array = myArray[2][1][0]index = 0array[index] = value


您可以考慮以下實(shí)現(xiàn):


function changeNeedle(value, keys) {

  let target = array;


  for (const i of keys.slice(0, -1)) {

    target = target[i];

  }


  target[keys[keys.length - 1]] = value;

}


查看完整回答
反對 回復(fù) 2022-08-27
  • 4 回答
  • 0 關(guān)注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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