-4

I have the following JSON data and would like to remove the duplicate first column 'time' using 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
}]

What's the easiest way to do that?

flppv
  • 4,111
  • 5
  • 35
  • 54

4 Answers4

2

You can use Array.prototype.map() and 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)
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
1

You can use delete keyword to remove a property from an object.

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);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

You should iterate through your array(I suggest .map(), and just delete time property from each object.

flppv
  • 4,111
  • 5
  • 35
  • 54
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);
})();

Here's the working code in jsfiddle. click here

mabhijith95a10
  • 436
  • 1
  • 4
  • 7