I'm working on an Ionic 3 project in which the response http data is in JSON array format like this (from the console): Country Array (5)
0: {record_id: "1", local_TimeStamp: "16:00:00", country: "USA"}
1: {record_id: "2", local_TimeStamp: "17:00:00", country: "Japan"}
2: {record_id: "3", local_TimeStamp: "17:00:00", country: "Korea"}
3: {record_id: "4", local_TimeStamp: "15:00:00", country: "Thailand"}
4: {record_id: "5", local_TimeStamp: "16:00:00", country: "China"}
How to (1) delete one of the above items (2) append to the last index of the above JSON array. Note: Due to some special view requirements, step 1 and 2 need to be separate. So the result JSON array will look like this:
0: {record_id: "1", local_TimeStamp: "16:00:00", country: "USA"}
1: {record_id: "2", local_TimeStamp: "17:00:00", country: "Japan"}
3: {record_id: "4", local_TimeStamp: "15:00:00", country: "Thailand"}
4: {record_id: "5", local_TimeStamp: "16:00:00", country: "China"}
2: {record_id: "3", local_TimeStamp: "17:00:00", country: "Korea"} <- Moved
I'd tried this code:
country.splice(country.findIndex(e => e.country === 'Korea'),1);
country = [...country];
// From console it's OK. Record deleted.
// Next append the element back:
country.push({record_id: "3", local_TimeStamp: "17:00:00", country: "Korea"});
country = [...country];
// From console looks OK. Element appended to the last index of the Json array.
But if I run the code again:
country.splice(country.findIndex(e => e.country === 'Korea'),1);
It can not find the element Korea anymore.