var arr = [[],[],null,[[],[]],[[[]]]];
$( document ).ready(function() {
addChild(arr, 'main');
});
function inscribe(numbers, value) {
while(numbers.length>1){
arr = arr[numbers.shift()];
}
arr[numbers.shift()] = value;
addChild(arr, 'main');
}
function addChild(subarr, id) {
var el = document.getElementById(id);
var ul = document.createElement("ul");
el.appendChild(ul);
subarr.forEach(function(item, index){
var node = document.createElement("div");
var textnode = document.createTextNode(id + '-' + index);
node.appendChild(textnode);
node.id = id + '-' + index;
ul.appendChild(node);
if(item && item.length)
addChild(item, node.id);
})
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="main"> </div>
<button onclick="inscribe([3,1], [[]])">inscribe([3,1], [[]])</button>
让我们假设我有一个nD数组,其值是动态变化的。
let arr = [[],[],[[],[]],null,[]];
。我需要一个接收数字数组和一个值的函数来更改主数组的值。
/*
* numbers: an array of integers which indicates which element of array should be set
* value: the value that should be set
*/
inscribe(numbers, value) {
// set value for desired position
}
例如,如果数字为[x,y,z]
,则应更改arr[x][y][z] = value
。
请注意,我们不知道numbers
数组的长度。
对实现该功能有何建议?
答案 0 :(得分:1)
您可以使用递归来获取/更改值;
func applicationWillTerminate(_ application: UIApplication) {
bgTask = application.beginBackgroundTask(withName:"Flink_logOut", expirationHandler: {() -> Void in
// Do something to stop our background task or the app will be killed
}
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}