当前位置: 首页 > 知识库问答 >
问题:

如何从javascript数组中删除最后一个索引元素?[重复]

谷梁振
2023-03-14

我有一个数组

0: {Premium: '18', ChangeType: 'REW', description: 'Replaced by Agent', TransDate: '2021-09-14 15:15:11', EffectiveDate: '2021-09-14', …}
1: {Premium: '19', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 15:10:45', EffectiveDate: '2021-09-14', …}
2: {Premium: '3', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 15:10:08', EffectiveDate: '2021-09-14', …}
3: {Premium: '19', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 15:09:43', EffectiveDate: '2021-09-14', …}
4: {Premium: '19', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 15:08:21', EffectiveDate: '2021-09-14', …}
5: {Premium: '19', ChangeType: 'XLN', description: 'ewqwe', TransDate: '2021-09-14 15:07:53', EffectiveDate: '2021-09-15', …}
6: {Premium: '198', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 15:01:01', EffectiveDate: '2021-09-14', …}
7: {Premium: '15', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 15:00:14', EffectiveDate: '2021-09-14', …}
8: {Premium: '19', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 14:59:08', EffectiveDate: '2021-09-14', …}
9: {Premium: '13', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 14:58:32', EffectiveDate: '2021-09-14', …}
10: {Premium: '11', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 14:57:14', EffectiveDate: '2021-09-14', …}
11: {Premium: '12', ChangeType: 'PCH', description: 'wewqe', TransDate: '2021-09-14 14:44:13', EffectiveDate: '2021-09-14', …}
12: {EntryDate: '9/14/2021', EffectiveDate: '2021-09-14', ChangeType: 'XLN', Premium: '19', description: 'Replaced by Agent', …}

我想从数组中删除最后一行。我使用了pop()函数,但它只返回最后一条记录,而不是返回除最后一条记录之外的完整数组。

此外,我尝试了拼接方法,如下所示。

policy_info.transactions.splice(policy_info.transactions.length - 1);

我无法得到结果。谁能告诉我用什么方法与数组来实现结果?

我想要的结果是

0: {Premium: '18', ChangeType: 'REW', description: 'Replaced by Agent', TransDate: '2021-09-14 15:15:11', EffectiveDate: '2021-09-14', …}
    1: {Premium: '19', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 15:10:45', EffectiveDate: '2021-09-14', …}
    2: {Premium: '3', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 15:10:08', EffectiveDate: '2021-09-14', …}
    3: {Premium: '19', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 15:09:43', EffectiveDate: '2021-09-14', …}
    4: {Premium: '19', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 15:08:21', EffectiveDate: '2021-09-14', …}
    5: {Premium: '19', ChangeType: 'XLN', description: 'ewqwe', TransDate: '2021-09-14 15:07:53', EffectiveDate: '2021-09-15', …}
    6: {Premium: '198', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 15:01:01', EffectiveDate: '2021-09-14', …}
    7: {Premium: '15', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 15:00:14', EffectiveDate: '2021-09-14', …}
    8: {Premium: '19', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 14:59:08', EffectiveDate: '2021-09-14', …}
    9: {Premium: '13', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 14:58:32', EffectiveDate: '2021-09-14', …}
    10: {Premium: '11', ChangeType: 'XLN', description: 'Replaced by Agent', TransDate: '2021-09-14 14:57:14', EffectiveDate: '2021-09-14', …}
    11: {Premium: '12', ChangeType: 'PCH', description: 'wewqe', TransDate: '2021-09-14 14:44:13', EffectiveDate: '2021-09-14', …}

共有1个答案

公瑞
2023-03-14

使用array.pop(),https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop

这将从使用此方法的数组中删除最后一项,并返回删除的项。

let array = [1,2,3];
let popper = array.pop(); // will be the removed value if you need it
console.log(array); // will log the array after the pop so [1,2]
 类似资料:
  • 问题内容: 我在数组上遇到了麻烦。我有一个要修改的数组,如下所示。我想通过索引删除它的元素(元素),然后重新索引数组。可能吗? 问题答案:

  • 我只需要数组Ex中的前两个索引:

  • 我有一个数组: 这些元素位于使用可拖动JS的vue组件中。Draggable有两个属性@start和@end。当我拖放数组中的元素时,将触发。每当我拖动其中的一个元素时,我都想移除当前元素前后的空数组。所以当我拖动[6]时,数组应该是这样的: isVisible在@start处触发,所以每当我选择一个元素时。如何移除数组中索引前后的元素?我以为用切片?

  • 如何从另一个数组中删除数组的元素?我有第一个数组[“a”“b”“c”]。以及第二阵列[“a”、“e”]、[“e”、“b”、“c”]、[“a”、“c”]。 如何从第二个数组中删除第一个数组的元素? 结果,得到[["e"],["e"], []].

  • 给定下面的代码: HTML: 断续器 我必须使用此代码才能删除元素吗?它似乎太冗长了。

  • 我有一个二维数组,其中最后的元素是空的。这样地: 现在我想删除空元素,并获得正确的新大小。长度 我试着这样做,但不起作用: 有什么好办法吗?