循环遍历对象并添加类型

时间:2016-06-17 14:54:11

标签: javascript

function arrayToList(array) {
  var list = null;
  for (var i = 0; i < array.length ; i++)
    list = {value: array[i], rest: list};
  return list;

}

console.log(arrayToList([10,20]));

为什么当我console.log这个数组时,我获得了value : 20rest: object,而不是value:10rest: null。这让我很困惑,因为我传入的数组的第一个元素是10.而我是从i = 0开始的。任何见解都会有所帮助感谢。

3 个答案:

答案 0 :(得分:2)

这是因为您将最后一个对象设置为list。要返回链表中的第一个,请在循环之前保留对它的引用,然后返回它。

虽然您的链接列表与数组相反。不确定这是不是你想要的。

如果要列出镜像阵列并向前链接,请执行以下操作:

&#13;
&#13;
function arrayToList(array) {
  var first = {value: array[0], rest: null};
  var prev = first;
  for (var i = 1; i < array.length ; i++)
    prev.rest = prev = {value: array[i], rest: null};

  return first;
}

console.log(arrayToList([10,20]));
&#13;
&#13;
&#13;

让我们假装我们没有循环,并且只是在内嵌整个事情。这就是它的样子。我也会这样做,好像数组是[10, 20, 30]

function arrayToList(array) {
  // Here `first` and `prev` reference the same object
  var first = {value: array[0], rest: null};
  var prev = first;

  // first = {value: 10, rest: null};
  // prev = {value: 10, rest: null};

  // Then we assign a new object to `prev.rest` as well as `prev`.
  // Because the `prev` and `first` variables reference the same object,
  // the mutation of `prev.rest` is also seen from `first.rest`.

  // First iteration
  prev.rest = prev = {value: array[1], rest: null};

  // now...
  // first = {value: 10, rest: (reference to object with value: 20)};
  // prev = {value: 20, rest: null};

  // So it first changed `prev.rest` (and therefore `first.rest`) to point to
  // the new object, but then it also updated `prev` to the same, new object.

  // Take note that though we assigned the new object to `prev.rest`, after
  // the assignment, `prev.rest` is actually `null`, because the `prev.rest`
  // that we assigned to was the `.rest` of the *previous* `prev`, which was
  // immediately updated to see the *new* object.

  // Second iteration
  prev.rest = prev = {value: array[2], rest: null};

  // now...
  // first = {value: 10, rest: (reference to object with value: 20)};
  // prev = {value: 30, rest: null};

  // So now when we changed `prev.rest`, it had no impact on `first` because
  // `prev` and `first` are referencing different objects. So `prev` sees the
  // new object, but `first` does not.

  return first;
}

答案 1 :(得分:1)

你正在用你的每次遍历覆盖休息的价值。我认为你应该使用递归函数的概念来使其快速准确。

请使用以下代码:

function arrayToList(array, i){
    if(i == array.length){
        return null ;
    }
    return { value: array[i], rest: arrayToList(array, i+1) };
}
console.log(arrayToList([10,20,30], 0));

这里,i表示数组中的索引。我希望这能解决你的问题。它在我的系统上运行良好。

我得到的结果是:

{
    value: 10, 
    rest: {
        value: 20, 
        rest: {
            value: 30, 
            rest:null
        }
    }
}

答案 2 :(得分:0)

您正在执行返回的&#34;列表&#34;的console.log();值。你不会看到&#34; null&#34;在list.rest中,因为列表的最终值如下所示: list = { value: 20, rest : { value : 10, rest : null } }; 如果你把以下列表放在list = {value:array [i]之后,rest:list};你会明白我的意思:
的console.log(list.rest)

console.log(list)如果你想看到整件事。

另外,回答你关于从你的第一个元素是10开始并从i = 0开始的问题:
数组[10,20]有一个array.length = 2
array [0] = 10
array [1] = 20

因此,最后一个值输入到&#34;列表&#34;将是值= 20.当你这样做时,你也存储&#34; list&#34;的值。在&#34; list.rest&#34;中,它将递归存储您最后的&#34;列表&#34; JSON对象是。如果你在列表中有超过2个项目,这可能会变得非常混乱,因为有3个,要到达第一个,你必须做list.rest.rest.value - 你可能想重新思考你是怎么做的#39 ;重新存储你的清单。