LHLK
LHLK

Reputation: 155

How to delete and append element in JSON array?

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.

Upvotes: 0

Views: 2435

Answers (5)

Scott Sauyet
Scott Sauyet

Reputation: 50787

Something like this might do it, although the indices you supply in your expected output don't make sense to me:

const originalCountries = [
  {record_id: "1", local_TimeStamp: "16:00:00", country: "USA"},
  {record_id: "2", local_TimeStamp: "17:00:00", country: "Japan"},
  {record_id: "3", local_TimeStamp: "17:00:00", country: "Korea"},
  {record_id: "4", local_TimeStamp: "15:00:00", country: "Thailand"},
  {record_id: "5", local_TimeStamp: "16:00:00", country: "China"}
];

const removeCountry = (name, countries) => {
  const idx = countries.findIndex(c => c.country === name);
  return idx > -1 
      ? {
           countries: countries.slice(0, idx).concat(countries.slice(idx + 1)), 
           country: countries[idx]
        }
      : {countries, country: null};
};

const addCountry = (country, countries) => countries.concat(country);

console.log('Step 1');
const {countries, country} = removeCountry('Korea', originalCountries);
console.log(countries);

console.log('Step 2');
const updatedCountries = addCountry(country, countries);
console.log(updatedCountries);

Upvotes: 0

Joshua Manns
Joshua Manns

Reputation: 585

It looks like you're doing some unnecessary destructuring with the spread operator. Here's a simple example using functions:

Example

let myCountries = [
  {record_id: "1", local_TimeStamp: "16:00:00", country: "USA"},
  {record_id: "2", local_TimeStamp: "17:00:00", country: "Japan"},
  {record_id: "3", local_TimeStamp: "17:00:00", country: "Korea"},
  {record_id: "4", local_TimeStamp: "15:00:00", country: "Thailand"},
  {record_id: "5", local_TimeStamp: "16:00:00", country: "China"}
];

function removeCountry(country, countries) {
  const index = countries.findIndex(c => c.country === country);
// Note: Country not found will mean index is -1
// which will remove the last country from the array.
  return countries.splice(index, 1);
}

function appendCountry(country, countries) {
  if (!country) return;

  countries.push(country);

  return countries;
}

// Usage:

const remove = removeCountry('Korea', myCountries);
appendCountry(remove);

Update I've given an example of how to use this and save the removed country to a variable for easier appending.

Upvotes: 1

Tomasz Bubała
Tomasz Bubała

Reputation: 2153

Renato Gama's comment might do the trick for you. But if you need to do these steps separately:

How to (1) delete one of the above items

You can use Array.prototype.filter to get an array without certain record, for example: myArray.filter(record => record.country !== "Korea"). But considering you want to add the record again, you could use Array.prototype.splice, like this:

var removed = myArray.splice(/* arguments */);

Above would be step 1 and step 2 would be pushing it again - myArray.push(removed);

But if I run the code again:

country.splice(country.findIndex(e => e.country === 'Korea'),1);

It can not find the element Korea anymore.

You can't find the element, since you removed it with splice and didn't save removed element to a variable.

Upvotes: 0

Ayoub
Ayoub

Reputation: 1435

you could do it like this:

let country = [
    { record_id: "1", local_TimeStamp: "16:00:00", country: "USA" },
    { record_id: "2", local_TimeStamp: "17:00:00", country: "Japan" },
    { record_id: "3", local_TimeStamp: "17:00:00", country: "Korea" },
    { record_id: "4", local_TimeStamp: "15:00:00", country: "Thailand" },
    { record_id: "5", local_TimeStamp: "16:00:00", country: "China" }
];
const korea = country.splice(country.findIndex(e => e.country === 'Korea'), 1);
country = [...country, ...korea];

Upvotes: 1

Faly
Faly

Reputation: 13346

Use array.prototype.slice, array.prototype.find and spread:

var arr = [
    {record_id: "1", local_TimeStamp: "16:00:00", country: "USA"},
    {record_id: "2", local_TimeStamp: "17:00:00", country: "Japan"},
    {record_id: "3", local_TimeStamp: "17:00:00", country: "Korea"},
    {record_id: "4", local_TimeStamp: "15:00:00", country: "Thailand"},
    {record_id: "5", local_TimeStamp: "16:00:00", country: "China"}
];

var index = arr.findIndex(e => e.country === "Korea");    
var a = arr.slice(0, index);
var b = arr.slice(index + 1);
var c = arr.find(e => e.country === "Korea");

var result = [...a, ...b, c];

console.log(result);

Upvotes: 0

Related Questions