Skip to content

Remove data containing string from object

I want to ask how do I remove strings from data. Lets say I have a data:

var data = {DeviceID: "101", ManufacturerID: "9", ManufacturerName: "Toshiba", Device Name: "Toshiba - Tecra R950", Price: "200"};

how do I remove ManufacturerName and Device Name because they do not have numbers?



The simplest method – if you’re happy with mutating the object rather than creating a new one – is to iterate over the object properties, and try to coerce each value to a number. If it’s not a number remove the property.

const data = { DeviceID: '101', ManufacturerID: '9', ManufacturerName: "Toshiba", 'Device Name': 'Toshiba - Tecra R950', Price: '200' };

for (let key in data) {
  if (!Number(data[key])) delete data[key];

User contributions licensed under: CC BY-SA
10 People found this is helpful