如何从JSON数据中删除列

时间:2019-03-15 06:32:04

标签: javascript json

我有以下JSON数据,并希望使用javascript删除重复的第一列“时间”

[{
    "time": "2019-03-14T07:03:45.348Z",
    "timestamp": "2019-01-11T12:00:00.0000000Z",
    "open": 6598,
    "high": 6616.5,
    "low": 6597,
    "close": 6613,
    "volume": 30635
}, {
    "time": "2019-03-15T06:01:15.612Z",
    "timestamp": "2019-01-14T12:00:00.0000000Z",
    "open": 6569,
    "high": 6575.5,
    "low": 6543.75,
    "close": 6552.25,
    "volume": 56948
}, {
    "time": "2019-03-15T06:05:46.389Z",
    "timestamp": "2019-01-14T12:00:00.0000000Z",
    "open": 6569,
    "high": 6575.5,
    "low": 6543.75,
    "close": 6552.25,
    "volume": 56948
}, {
    "time": "2019-03-15T06:05:46.403Z",
    "timestamp": "2019-01-14T16:00:00.0000000Z",
    "open": 6552,
    "high": 6570,
    "low": 6540,
    "close": 6567,
    "volume": 19266
}]

最简单的方法是什么?

4 个答案:

答案 0 :(得分:2)

您可以使用Array.prototype.map()Rest in Object Destructuring

let arr = [{"time":"2019-03-14T07:03:45.348Z","timestamp":"2019-01-11T12:00:00.0000000Z","open":6598,"high":6616.5,"low":6597,"close":6613,"volume":30635},
{"time":"2019-03-15T06:01:15.612Z","timestamp":"2019-01-14T12:00:00.0000000Z","open":6569,"high":6575.5,"low":6543.75,"close":6552.25,"volume":56948},
{"time":"2019-03-15T06:05:46.389Z","timestamp":"2019-01-14T12:00:00.0000000Z","open":6569,"high":6575.5,"low":6543.75,"close":6552.25,"volume":56948},
{"time":"2019-03-15T06:05:46.403Z","timestamp":"2019-01-14T16:00:00.0000000Z","open":6552,"high":6570,"low":6540,"close":6567,"volume":19266}]


let newArr = arr.map(({time,...rest}) => rest);
console.log(newArr)

答案 1 :(得分:1)

您可以使用delete关键字从对象中删除属性。

let json = `[{"time":"2019-03-14T07:03:45.348Z","timestamp":"2019-01-11T12:00:00.0000000Z","open":6598,"high":6616.5,"low":6597,"close":6613,"volume":30635},{"time":"2019-03-15T06:01:15.612Z","timestamp":"2019-01-14T12:00:00.0000000Z","open":6569,"high":6575.5,"low":6543.75,"close":6552.25,"volume":56948},{"time":"2019-03-15T06:05:46.389Z","timestamp":"2019-01-14T12:00:00.0000000Z","open":6569,"high":6575.5,"low":6543.75,"close":6552.25,"volume":56948},{"time":"2019-03-15T06:05:46.403Z","timestamp":"2019-01-14T16:00:00.0000000Z","open":6552,"high":6570,"low":6540,"close":6567,"volume":19266}]`;

// parse the JSON string
var arr = JSON.parse(json);

// iterate and delete time property
arr.forEach(o => delete o.time);

// convert back to JSON string
json = JSON.stringify(arr);

console.log(json);

答案 2 :(得分:0)

您应该遍历数组(我建议使用.map(),并从每个对象中仅获取delete time属性。

答案 3 :(得分:0)

(function() {
  var data = [{
      "time": "2019-03-14T07:03:45.348Z",
      "timestamp": "2019-01-11T12:00:00.0000000Z",
      "open": 6598,
      "high": 6616.5,
      "low": 6597,
      "close": 6613,
      "volume": 30635
    },
    {
      "time": "2019-03-14T07:03:45.348Z",
      "timestamp": "2019-01-11T12:00:00.0000000Z",
      "open": 6598,
      "high": 6616.5,
      "low": 6597,
      "close": 6613,
      "volume": 30635
    }
  ];

  for (var i = 0; i < data.length; i++) {
    delete data[i].time;
  }

  console.log(data);
})();

这是jsfiddle中的工作代码。 click here