如何在JSON数组中删除和追加元素?

时间:2018-03-21 14:16:55

标签: javascript arrays

我正在开发一个Ionic 3项目,其中响应http数据采用JSON数组格式(来自控制台):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"}

如何(1)删除上述项之一(2)附加到上述JSON数组的最后一个索引。注意:由于一些特殊的视图要求,步骤1和2需要分开。所以结果JSON数组看起来像这样:

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

我试过这段代码:

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.

但如果我再次运行代码:

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

再也找不到韩国元素了。

5 个答案:

答案 0 :(得分:1)

看起来你正在使用扩展运算符进行一些不必要的解构。这是一个使用函数的简单示例:

示例

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);

<强>更新 我给出了一个如何使用它并将删除的国家保存到变量以便于追加的示例。

答案 1 :(得分:1)

你可以这样做:

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];

答案 2 :(得分:0)

使用array.prototype.slicearray.prototype.findspread

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

Renato Gama的评论可能会为您解决问题。但是如果你需要单独执行这些步骤:

  

如何(1)删除上述项目之一

您可以使用Array.prototype.filter获取没有特定记录的数组,例如:myArray.filter(record => record.country !== "Korea")。但考虑到您想再次添加记录,可以使用Array.prototype.splice,如下所示:

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

上面是第1步,第2步将再次推动它 - myArray.push(removed);

  

但如果我再次运行代码:

     

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

     

再也找不到韩国元素了。

您找不到该元素,因为您使用splice将其删除,并且未将已删除的元素保存到变量中。

答案 4 :(得分:0)

这样的事情可能会这样做,虽然你在预期输出中提供的指数对我没有意义:

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);