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

Posted

技术标签:

【中文标题】如何从javascript中的数组中删除最后一个索引元素? [复制]【英文标题】:How to remove last index element from array in javascript? [duplicate] 【发布时间】:2021-11-09 12:26:03 【问题描述】:

我有一个数组

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', …

【问题讨论】:

这能回答你的问题吗? Remove last item from array @victorfau 否 它对我没有帮助,它只返回这样的一行 0: EntryDate: '9/14/2021', EffectiveDate: '2021-09-14', ChangeType: 'XLN ',Premium:'11',描述:'被代理替换',... pop 返回最后一个条目并就地修改数组 @Alnitak 是的,pop 只会返回最后一个条目,但我想删除最后一个条目。所以它没有帮助。 @SunilSethi 不,它还会删除最后一个条目 【参考方案1】:

使用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]

【讨论】:

但在我的情况下它只返回,0: EntryDate: '9/14/2021', EffectiveDate: '2021-09-14', ChangeType: 'XLN', Premium: '19' , 描述: '被代理替换', ... 我需要除此之外的其余条目。 @SunilSethi splice 返回一个包含已删除元素的数组。 pop 只返回被移除的元素。所以你不可能得到0: pop @SunilSethi 在阵列上使用pop 后,再次尝试访问阵列并查看有多少条目。您正在访问 pop 方法的返回值,此时您应该在使用 pop 后查看数组 @Pauliecode 是的,你是对的。那是我犯的错误。现在我得到了想要结果的数组

以上是关于如何从javascript中的数组中删除最后一个索引元素? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何从 JavaScript 中的数组中删除元素?

jQuery从数组中删除最后一个逗号

如何从数组中删除除javascript中的第一个元素之外的所有元素

javascript 删除并返回数组中的最后一个元素

Javascript - 如何从数组列表中获取最后一条记录? [复制]

javascript中,知道一个值,如何删除数组中的这个值的元素。