简体   繁体   English

从 json 阵列中移除元素

[英]Removing element from json array

I have a Json file which i am using to store data but i can't remove something from it without it putting "null," where that element was.我有一个 Json 文件,我用它来存储数据,但如果不将“null”放在该元素所在的位置,我就无法从中删除某些内容。 I've seen people using splice but it doesn't work for me.我见过有人使用拼接,但它对我不起作用。 This is what happens when i use splice这就是我使用拼接时发生的情况

The code i used was delete JsonObj.Times[index] and JsonObj.Times.splice(index, 1)我使用的代码是删除 JsonObj.Times[index] 和 JsonObj.Times.splice(index, 1)

Input输入

{
    "Times": [
        {
            "TimeStamp": "1588516643",
            "PremiumTime": "1",
            "ID": "473873947895897",
            "GuildID": "27823978489723789"
        },
        {
            "TimeStamp": "1588516643",
            "PremiumTime": "1",
            "ID": "473873947895897",
            "GuildID": "27823978489723789"
        }
    ]
}

What It Does它能做什么

[
  {
    "TimeStamp": "1588516643",
    "PremiumTime": "1",
    "ID": "473873947895897",
    "GuildID": "27823978489723789"
  }
]

What I Want我想要的是

{
    "Times": [
        {
            "TimeStamp": "1588516643",
            "PremiumTime": "1",
            "ID": "473873947895897",
            "GuildID": "27823978489723789"
        }
    ]
}

When i use delete it does what i want but puts null or undefined in the array.当我使用 delete 时,它会执行我想要的操作,但会将 null 或 undefined 放入数组中。

If you want to use splice() , you can do something like this:如果你想使用splice() ,你可以这样做:

 const obj = { "Times": [ { "TimeStamp": "1588516643", "PremiumTime": "1", "ID": "473873947895897", "GuildID": "27823978489723789" }, { "TimeStamp": "1588516643", "PremiumTime": "2", "ID": "473873947895897", "GuildID": "27823978489723789" } ] } const res = obj.Times.splice(0, 1); console.log(obj);

splice() 拼接()

If you know where the indexed values are going to end up, or are trying to target first/last values, you can also use the common array manipulation methods, .shift() , or .pop() .如果您知道索引值将在哪里结束,或者试图定位第一个/最后一个值,您还可以使用常见的数组操作方法.shift().pop()

If I correctly understand, you want to delete a determination element from Times's array.如果我理解正确,您想从 Times 的数组中删除一个确定元素。

Let say you have a variable that contains JSON data.假设您有一个包含 JSON 数据的变量。 It means that you can not have access to the data that are storing the variable because it is of type string.这意味着您无法访问存储变量的数据,因为它是字符串类型。

const jsonData = "{
"Times": [
    {
        "TimeStamp": "1588516643",
        "PremiumTime": "1",
        "ID": "473873947895897",
        "GuildID": "27823978489723789"
    },
    {
        "TimeStamp": "1588516643",
        "PremiumTime": "1",
        "ID": "473873947895897",
        "GuildID": "27823978489723789"
    }
  ]
 }"

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse来源: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

First of all, you have to parse JSON data using JSON.parse() that will convert the data to a JavaScript value or object described by the string. First of all, you have to parse JSON data using JSON.parse() that will convert the data to a JavaScript value or object described by the string. This will allow you to interact with this data.这将允许您与这些数据进行交互。

let parsedData = JSON.parse(jsonData)

It will create a new variable with parsed data and it contains an object with an array of objects inside of it.它将使用已解析的数据创建一个新变量,其中包含一个 object,其中包含一个对象数组。

parsedData = {
Times: [
    {
        "TimeStamp": "1588516643",
        "PremiumTime": "1",
        "ID": "473873947895897",
        "GuildID": "27823978489723789"
    },
    {
        "TimeStamp": "1588516643",
        "PremiumTime": "1",
        "ID": "473873947895897",
        "GuildID": "27823978489723789"
    }
  ]
 }

Second, we will remove the element by index from Times's array that are declared inside our object parsedData.其次,我们将从 Times 数组中按索引删除元素,这些元素在 object parsedData 中声明。

parsedData.Times.split(index,1) 

Where: first parameter "index": The index at which to start changing the array.其中: 第一个参数“index”:开始更改数组的索引。 second parameter "1": An integer indicating the number of elements in the array to remove from the start.第二个参数“1”:一个 integer 表示数组中要从开头删除的元素数。

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice来源: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Hope this can help you!希望这可以帮到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM