为什么array.splice总是删除最后一个元素?

时间:2018-06-15 05:28:00

标签: javascript

我这里有这段代码

 cart.splice(1, 1);      

其中cart是一个数组。无论我在splice的第一个元素中给出什么,它总是删除最后一个元素。如何解决它,为什么会发生? 我的数组看起来像这样: -

[{
  "productId": "400002",
  "productQuantity": 3,
  "productName": "Annadata Organic Brinjal Long Purple",
  "productPrice": "7",
  "productCategory": "Fruits & Vegetables",
  "shopId": "10375",
  "shopName": "Dubori",
  "shopDelivery": "1",
  "delivery": 1,
  "variant": "{\"id\":400002,\"image\":\"https:\\\/\\\/s3.ap-south-1.amazonaws.com\\\/doorhopperimg\\\/products\\\/83c7dc09da8eceae96fd9bcbd819e43b.jpg\",\"quantity\":\"250gm\",\"price\":7,\"afterTax\":null,\"stock\":-2,\"description\":\"Annadata Organic Brinjal Long Purple      Rs.7\\\/250gm\",\"barcode\":\"10375400002\"}"
}, {
  "productId": "400006",
  "productQuantity": "1",
  "productName": "Apple (Kashmiri)",
  "productPrice": "33",
  "productCategory": "Fruits & Vegetables",
  "shopId": "10375",
  "shopName": "Dubori",
  "shopDelivery": "1",
  "delivery": 1,
  "variant": "{\"id\":400006,\"image\":\"https:\\\/\\\/s3.ap-south-1.amazonaws.com\\\/doorhopperimg\\\/products\\\/07e8be62843d3c9145d572df70fe06d4.jpg\",\"quantity\":\"250gm\",\"price\":33,\"afterTax\":null,\"stock\":-2,\"description\":\"Apple (Kashmiri)      Rs.33\\\/250gm\",\"barcode\":\"10375400006\"}"
}]

现在它只有两个元素,但还有更多

2 个答案:

答案 0 :(得分:1)

可能想要了解有关拼接功能的更多信息。

以下是https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

的引用

var months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at 1st index position
console.log(months);
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'June']

months.splice(4, 1, 'May');
// replaces 1 element at 4th index
console.log(months);
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'May']

因此,在您的情况下,此代码段可能会对您有所帮助。请记住,如果要插入,只需splice(index, 0, item) 0。

var array = [{
  "productId": "400002",
  "productQuantity": 3,
  "productName": "Annadata Organic Brinjal Long Purple",
  "productPrice": "7",
  "productCategory": "Fruits & Vegetables",
  "shopId": "10375",
  "shopName": "Dubori",
  "shopDelivery": "1",
  "delivery": 1,
  "variant": "{\"id\":400002,\"image\":\"https:\\\/\\\/s3.ap-south-1.amazonaws.com\\\/doorhopperimg\\\/products\\\/83c7dc09da8eceae96fd9bcbd819e43b.jpg\",\"quantity\":\"250gm\",\"price\":7,\"afterTax\":null,\"stock\":-2,\"description\":\"Annadata Organic Brinjal Long Purple      Rs.7\\\/250gm\",\"barcode\":\"10375400002\"}"
}, {
  "productId": "400006",
  "productQuantity": "1",
  "productName": "Apple (Kashmiri)",
  "productPrice": "33",
  "productCategory": "Fruits & Vegetables",
  "shopId": "10375",
  "shopName": "Dubori",
  "shopDelivery": "1",
  "delivery": 1,
  "variant": "{\"id\":400006,\"image\":\"https:\\\/\\\/s3.ap-south-1.amazonaws.com\\\/doorhopperimg\\\/products\\\/07e8be62843d3c9145d572df70fe06d4.jpg\",\"quantity\":\"250gm\",\"price\":33,\"afterTax\":null,\"stock\":-2,\"description\":\"Apple (Kashmiri)      Rs.33\\\/250gm\",\"barcode\":\"10375400006\"}"
}];
console.log(array);

array.splice(1, 0, ["hi"]);
console.log(array);

此行array.splice(1, 0, ["hi"]);在位置1插入[“hi”]。

然而,array.splice(1, 1, ["hi"]);用[“hi”]

替换位置1中的东西

array.slice(1)删除第一个元素

答案 1 :(得分:0)

只需使用arr.slice(startingIndex, endingIndex)

如果未指定endingIndex,则返回从提供的索引开始的所有项目。

在您的情况下arr=arr.slice(1)



var cart=[{"productId":"400002","productQuantity":3,"productName":"Annadata Organic Brinjal Long Purple","productPrice":"7","productCategory":"Fruits & Vegetables","shopId":"10375","shopName":"Dubori","shopDelivery":"1","delivery":1,"variant":"{\"id\":400002,\"image\":\"https:\\\/\\\/s3.ap-south-1.amazonaws.com\\\/doorhopperimg\\\/products\\\/83c7dc09da8eceae96fd9bcbd819e43b.jpg\",\"quantity\":\"250gm\",\"price\":7,\"afterTax\":null,\"stock\":-2,\"description\":\"Annadata Organic Brinjal Long Purple      Rs.7\\\/250gm\",\"barcode\":\"10375400002\"}"},{"productId":"400006","productQuantity":"1","productName":"Apple (Kashmiri)","productPrice":"33","productCategory":"Fruits & Vegetables","shopId":"10375","shopName":"Dubori","shopDelivery":"1","delivery":1,"variant":"{\"id\":400006,\"image\":\"https:\\\/\\\/s3.ap-south-1.amazonaws.com\\\/doorhopperimg\\\/products\\\/07e8be62843d3c9145d572df70fe06d4.jpg\",\"quantity\":\"250gm\",\"price\":33,\"afterTax\":null,\"stock\":-2,\"description\":\"Apple (Kashmiri)      Rs.33\\\/250gm\",\"barcode\":\"10375400006\"}"}];

var output=cart.slice(1);

console.log(output);