【问题标题】:Javascript .push causing array to nest deeper and deeper [duplicate]Javascript .push 导致数组嵌套越来越深[重复]
【发布时间】:2019-09-11 17:35:40
【问题描述】:

我确信这很容易,但是我的搜索还没有帮助。

当我将一个数组推入另一个数组时,它们会嵌套更深一层。第一个数组位于正确的深度。

var productArray = [{productID: currentProduct, productPrice: currentPrice, productName: productName, options: myOptions}];

if(localStorage.getItem("cart")){
    var existingArray = JSON.parse(localStorage.getItem("cart"));
    existingArray.push(productArray);
    localStorage.setItem("cart", JSON.stringify(existingArray));
} else {
    localStorage.setItem("cart", JSON.stringify(productArray));
}

结果:

0: Object { productID: "1", productPrice: "2.00", productName: "Chicken Sandwich", … }
​
1: 0: Object { productID: "1", productPrice: "2.00", productName: "Chicken Sandwich", … }
​
2: 0: Object { productID: "1", productPrice: "2.00", productName: "Chicken Sandwich", … }

【问题讨论】:

  • 这就是假定会发生的事情,你为什么要不然呢?
  • @jonrsharpe 从第 2 个数组开始,数组要深 1 级。
  • @M_Becker concat 不修改任何一个数组;它返回一个 new 数组。您可能希望将其存储为新变量,即var newArray = array1.concat(array2),其中newArrayarray1array2 的合并结果。
  • @TylerRoper ... 工作。我今天学到了新东西,谢谢!您想将其作为答案提交,以便我将其标记为已解决吗?

标签: javascript arrays multidimensional-array


【解决方案1】:

您有多个选项来连接两个数组,但.push 不是(除非您迭代第二个数组并单独推送每个元素,或者跟进.flat()

let array = [1, 2, 3];

array.push([4, 5, 6]);
console.log(array); // [1, 2, 3, [4, 5, 6]]

array = array.concat([7, 8, 9]);
console.log(array); // [1, 2, 3, [4, 5, 6], 7, 8, 9]

array = [...array, ...[10, 11, 12]];
console.log(array); // [1, 2, 3, [4, 5, 6], 7, 8, 9, 10, 11, 12]

array.push(13, 14, 15);
array = array.flat();
console.log(array); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

【讨论】:

  • 嗨君瓦。将来,考虑标记重复而不是回答它们。提交已经被问过很多问题的答案往往会使网站因重复信息而变得混乱,并鼓励用户提出重复问题:)
【解决方案2】:

因为 productArray 是一个数组,而不是一个对象,将它推入另一个数组将创建 另一个 数组,其中将包含初始数组和新数组一个。

如您所愿,您应该声明一个新对象,并将 it 推送到现有数组中。

var productObject = {productID: 1, productPrice: 1, productName: '', options:{}};
var productArray = [];
if(localStorage.getItem("cart")){
    var existingArray = JSON.parse(localStorage.getItem("cart"));
    existingArray.push(productObject );
    localStorage.setItem("cart", JSON.stringify(existingArray));
} else {
    productArray.push(productObject);
    localStorage.setItem("cart", JSON.stringify(productArray));
}

【讨论】:

    【解决方案3】:

    我认为您将整个“productArray”添加到“existingArray”的位置 n 中。试着做这个。

    var productArray = [{productID: currentProduct, productPrice: currentPrice, productName: productName, options: myOptions}];
    
    if(localStorage.getItem("cart")){
        var existingArray = JSON.parse(localStorage.getItem("cart"));
        productArray.forEach(function(val){
         existingArray.push(val);
        })
        localStorage.setItem("cart", JSON.stringify(existingArray));
    } else {
        localStorage.setItem("cart", JSON.stringify(productArray));
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-05
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 2015-11-20
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多