Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
雄辩的JavaScript:第4章_Javascript_Arrays - Fatal编程技术网

雄辩的JavaScript:第4章

雄辩的JavaScript:第4章,javascript,arrays,Javascript,Arrays,我需要帮助逐步了解此解决方案是如何实现链接列表的 function arrayToList(array) { let result = {}; if (Array.isArray(array)) { let currListItem = result; for (let item of array) { let newListItem = { value: item, rest: null }; if (

我需要帮助逐步了解此解决方案是如何实现链接列表的

function arrayToList(array) {
  let result = {};
  if (Array.isArray(array)) {
    let currListItem = result;
    for (let item of array) {
      let newListItem = {
        value: item,
        rest: null
      };
      if (typeof currListItem.rest === 'undefined') {
        result = newListItem;
      } else {
        currListItem.rest = newListItem;
      }
      currListItem = newListItem;
    }
  }
  return result;
}
另见:


首先检查给定的参数数组是否实际上是一个数组,
array.isArray(array)

如果是,则为
currListItem
分配一个空对象

现在迭代数组的每个项,并为每个项创建一个具有2个属性的新对象,值存储该项,其余项初始化为null

然后在这一行
如果(typeof currListItem.rest==='undefined')
,检查currListItem对象中是否有rest属性。这样做是为了检查这是否是链接列表中的第一个节点

function arrayToList(array) {
  let result = {};
  if (Array.isArray(array)) {
    let currListItem = result;
    for (let item of array) {
      let newListItem = {
        value: item,
        rest: null
      };
      if (typeof currListItem.rest === 'undefined') {
        result = newListItem;
      } else {
        currListItem.rest = newListItem;
      }
      currListItem = newListItem;
    }
  }
  return result;
}
如果If条件为true,则将节点newListItem分配给结果。然后将
newListItem
分配给
currlist
,并继续进行进一步的迭代

如果在第一次迭代后if条件变为false,我们需要将新对象与现有节点链接。因此,使用此行
currListItem.rest=newListItem,我们正在将新对象链接到上一个节点的rest字段


最后,我们将这个新节点标记为
currListItem
,以便下一次迭代

属性名称应该是下一个,而不是rest,因为它将使理解更容易。

“最后,我们将此新节点标记为currListItem,以便下一次迭代。”


这是怎么回事?似乎要继续构建链接列表,您不应该将其设置为新节点?

请发布函数的实际文本,而不是屏幕截图。这个问题与git
有什么关系?我删除了
git
标签。。。