简体   繁体   English

从JSON对象中删除所有键

[英]Remove all keys from JSON object

Initially I was working with a CSV, where each row contained data eg 最初我使用的是CSV,其中每一行都包含数据,例如

------------------------------------------------------
123            |   cat               |   dog         |   
------------------------------------------------------
456            |   cat               |   rabbit      |   
------------------------------------------------------
789            |   snake             |   dog         |   
------------------------------------------------------

I am now getting more data with different structure, so I can no longer use a csv. 我现在正在以不同的结构获取更多数据,因此无法再使用csv。 Instead I am using JSON file. 相反,我正在使用JSON文件。 The JSON file looks something like this JSON文件如下所示

[
  [
    {
      "ID": 123,
      "animal_one": "cat",
      "animal_two": "dog"
    },
    {
      "ID": 456,
      "animal_one": "cat",
      "animal_two": "rabbit"
    },
    {
      "ID": 789,
      "animal_one": "snake",
      "animal_two": "dog"
    }
  ],
  [
    2222
  ],
  [
    12345
  ],
  [
    "2012-01-02"
  ],
  [
    "2012-12-20"
  ]
]

So you can see the additional data. 这样您就可以看到其他数据。 For this part of the application, I only want to work with the first part of the JSON, the part containing animals. 对于应用程序的这一部分,我只想处理JSON的第一部分,即包含动物的部分。 The function I have basically works on values only, so I need to make this JSON like the original CSV file, whereby I only have the values, no keys. 我拥有的函数基本上只对值起作用,因此我需要使JSON像原始CSV文件一样,其中我只有值,没有键。

So I am loading the JSON file, which is then contained within the variable data 所以我正在加载JSON文件,该文件随后包含在变量data

I am then trying something like this 然后我正在尝试这样的事情

var key = "CUST_ID";
delete data[0][key];
console.log(JSON.stringify(data[0]))

Although this doesnt even work, I think it is the wrong approach anyway. 尽管这甚至行不通,但我仍然认为这是错误的方法。 I dont want to define the keys I want removed, I just want it to remove all keys, and keep the values. 我不想定义要删除的键,我只希望它删除所有键并保留值。

How would I go about doing this? 我将如何去做呢? I am using data[0] to get the first section of the JSON, the part that contains animals. 我正在使用data[0]来获取JSON的第一部分,该部分包含动物。 Not sure if this is correct either? 不确定这是否正确?

Thanks 谢谢

I dont want to define the keys I want removed 我不想定义要删除的键

You still will need to define which keys you want the values from. 你仍然需要定义你想要的值哪个键。 One could just take all the values from the object, in arbitrary order, but that's not robust against changes to the data format. 可以以任意顺序获取对象中的所有值,但这对于更改数据格式并不可靠。 Use something like 使用类似

const table = data[0].map(value => [value.ID, value.animal_one, value.animal_two]);

You can simply do this, if you dont care what keys you are getting: 如果您不在乎所获得的密钥,则可以简单地执行此操作:

var collection = "";
data[0].forEach(function(row) {
    var line = [];
    Object.keys(row).forEach(function(key) {
        line.push(row[key])
    });
    collection = collection + line.join(',') + '\n';
})

You will get csv string out of collection 您将失去收藏的csv字符串

data[0].forEach(function(row,index) {
   data[0][index] = Object.values(data[0][index]);
});

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

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