-3

I have an array

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

I want to remove the last row from the array. I had used the pop() function but it returns only the last record rather than returning the full array except the last one.

Also, I had tried with the splice method like below.

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

I could not get the result. Can anyone tell me what method to use with the array to achieve the result?

My Desired result is

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', …}
  • 2
    Does this answer your question? [Remove last item from array](https://stackoverflow.com/questions/19544452/remove-last-item-from-array) – victorfau Sep 14 '21 at 10:04
  • @victorfau No It does not help me it return only one row like this 0: {EntryDate: '9/14/2021', EffectiveDate: '2021-09-14', ChangeType: 'XLN', Premium: '11', description: 'Replaced by Agent', …} – Sunil Sethi Sep 14 '21 at 10:06
  • 3
    `pop` returns the last entry and _modifies the array in place_. – Alnitak Sep 14 '21 at 10:07
  • @Alnitak yes, pop will return only the last entry but I want to remove the last entry. so it does not help. – Sunil Sethi Sep 14 '21 at 10:09
  • @SunilSethi No, it also removes the last entry – schlonzo Sep 14 '21 at 10:11
  • @SunilSethi `pop` will remove the last entry from the array. `pop` also returns the last entry from the array if you need it, but the last entry will be removed from the array anyways. – Pauliecode Sep 14 '21 at 10:12
  • @SunilSethi yes that's exactly what it does. The _in place modification_ is the removal of that last entry. – Alnitak Sep 14 '21 at 10:14

1 Answers1

2

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

This will remove the last item from the array that this method is used on and return the removed item.

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]
  • But In my case It return only , 0: {EntryDate: '9/14/2021', EffectiveDate: '2021-09-14', ChangeType: 'XLN', Premium: '19', description: 'Replaced by Agent', …} and I need the rest entries except only this. – Sunil Sethi Sep 14 '21 at 10:11
  • @SunilSethi `splice` returns an array with the removed elements. `pop` only returns the removed element. So it’s impossible that you get `0: {`…`}` with `pop`. – Sebastian Simon Sep 14 '21 at 10:13
  • 1
    @SunilSethi After using `pop` on the array, try accessing the array again and see how many entries there are. You are accessing the return of the `pop` method, when you should be looking at the array after using `pop` – Pauliecode Sep 14 '21 at 10:15
  • @Pauliecode Yes, you are right. that's the mistake I made. Now I get the array with desired result – Sunil Sethi Sep 14 '21 at 10:19