简体   繁体   English

如何仅在循环的第一次迭代中创建一个空数组然后继续使用它?

[英]How to create an empty array only in first iteration of loop and then keep using it?

I am new to javascript .我是javascript 新手 I have two functions, one called arrayToList which takes an array and produces a list from it, the other is called listToArray which takes a list and produces array from it.我有两个函数,一个叫做 arrayToList,它接受一个数组并从中生成一个列表,另一个叫做listToArray ,它接受一个列表并从中生成一个数组。 first one is working fine.第一个工作正常。 However, in case of second one I have to create an empty array in the first iteration only of a for or while loop within which I will pass on each value of list to array I have created and at each step remove list.value and set the list to list.rest (that is rest of list) and then recursively keep calling the same function.但是,在第二个的情况下,我必须在 for 或 while 循环的第一次迭代中创建一个空数组,在该循环中我会将列表的每个值传递给我创建的数组,并在每个步骤中删除 list.value 并设置列表到 list.rest (即列表的其余部分),然后递归地继续调用相同的函数。

My problem is that I am not able to declare the array within the function.我的问题是我无法在函数中声明数组。 If I say if(!array) {let array=[];} then I get message that array is undefined, if I declare array as global then it is retaining values across different calls to function, which I don't want.如果我说if(!array) {let array=[];}然后我得到消息说数组未定义,如果我将数组声明为全局,那么它会在不同的函数调用中保留值,这是我不想要的。 Below is the code.下面是代码。 Thanks for your help.谢谢你的帮助。

let array=[];


function listToArray(list) {
  
  while(list.value) {
    
    array.push(list.value);
    delete list.value;
    list=list.rest;
    listToArray(list);
    
   }
  
  return array;
 
  
}

console.log(listToArray({value: 10, rest: {value: 20, rest: {
value:  30
rest:   {value: 50, rest: {value: 70, …}}));

You could take a recursive approach without using an array for collecting nested items.您可以采用递归方法而不使用数组来收集嵌套项。

 function nestedObjToArray({ value, rest }) { return [value, ...(rest ? nestedObjToArray(rest) : [])]; } console.log(nestedObjToArray({ value: 10, rest: { value: 20, rest: { value: 30, rest: { value: 50, rest: { value: 70 } } } } }));

I'd use a default 2nd parameter that defaults to the empty array, then pass it along:我将使用默认为空数组的默认第二个参数,然后将其传递:

 function nestedObjToArray(obj, arr = []) { arr.push(obj.value); if (obj.rest) { nestedObjToArray(obj.rest, arr); } return arr; } console.log(nestedObjToArray({ value: 10, rest: { value: 20, rest: { value: 30, rest: { value: 50, rest: { value: 70, } } } } }))

This way, the array gets created the first time the function is called, because the function isn't initially called with a second parameter.这样,数组在第一次调用函数时被创建,因为函数最初不是用第二个参数调用的。 On subsequent recursive calls, the created array is passed down as the second parameter.在随后的递归调用中,创建的数组作为第二个参数向下传递。

Note that "list" is not something with a generally-understood universal meaning in JS.请注意,“列表”在 JS 中并不是具有普遍理解的通用含义的东西。 Some might think you're referring to a plain array.有些人可能认为您指的是普通数组。 Since what you're passing is a nested object, you might call the function nestedObjToArray or something similar.由于您传递的是嵌套对象,因此您可以调用函数nestedObjToArray或类似的东西。

If you can't pass another parameter, you can create the array on the final recursive call and return it:如果你不能传递另一个参数,你可以在最后的递归调用中创建数组并返回它:

 function nestedObjToArray(obj) { const arr = obj.rest ? nestedObjToArray(obj.rest) : []; arr.unshift(obj.value); return arr; }; console.log(nestedObjToArray({ value: 10, rest: { value: 20, rest: { value: 30, rest: { value: 50, rest: { value: 70, } } } } }))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM