-2

I have the following Object

{date_created: 1546293600000, num: 370, num2: NaN, num3: 100, category: "C"}

I would my output to be

{num: 370, num3: 100}

Removing date_created, num2, and category ideally it would remove also anything that is null or undefined in the value

Almog
  • 2,639
  • 6
  • 30
  • 59
  • 5
    But date_created is a number. That aside, see https://stackoverflow.com/questions/30726830/how-to-filter-keys-of-an-object-with-lodash – jarmod Dec 18 '19 at 13:23
  • @jarmod that's fine if he's already using lodash for something else. But I wouldn't advise anyone to include a library for a such simple task – Christian Vincenzo Traina Dec 18 '19 at 13:29
  • You want to remove `date_created`, `num2` and `category` irrespective of their value and in addition to those, any key with null or undefined value? It's not exactly clear – adiga Dec 18 '19 at 13:33
  • @CristianTraìna agreed, you can solve this readily in native JavaScript these days. – jarmod Dec 18 '19 at 13:37

6 Answers6

2
Object.keys(obj).filter(key => !isNaN(obj[key]))

let obj = {
  date_created: 1546293600000,
  num: 370,
  num2: NaN,
  num3: 100,
  category: "C"
}

let keys = Object.keys(obj).filter(key => !isNaN(obj[key]))
let newObj = {}
keys.forEach(key => newObj[key] = obj[key])
console.log(newObj)
kooskoos
  • 4,622
  • 1
  • 12
  • 29
2

You can use filter to filter out all the NaN values (Not A Number, so also strings, objects, etc). Then re-map the keys to get the values:

const obj = {date_created: 1546293600000, num: 370, num2: NaN, num3: 100, category: "C"}

const newObj = Object.keys(obj)
  .filter(key => !isNaN(obj[key]))
  .reduce((acc, key) => {
    acc[key] = obj[key];
    return acc;
  }, {});

  
console.log(newObj);

In your example you filtered out date_created but what's your rationale?

jarmod
  • 71,565
  • 16
  • 115
  • 122
1

You can do this with the Object.keys property and a filter

var myObject = {
  date_created: 1546293600000,
  num: 370,
  num2: NaN,
  num3: 100,
  category: "C"
};

var newObject = Object.keys(myObject)
.filter(key => {
  return !isNaN(myObject[key]);
}).reduce((output, key) => {
  output[key] = obj[key];
  return output;
}, {});

In regards to the "date_created" property, you could add something like this:

Ignore all properties that have "date" in their names, even if their values are numbers.

var newObject = Object.keys(myObject)
.filter(key => {
  if (key.includes("date")) return false;
  return !isNaN(myObject[key]);
}).reduce((output, key) => {
  output[key] = obj[key];
  return output;
}, {});
ZektorH
  • 2,680
  • 1
  • 7
  • 20
  • 1
    You can also use `Object.values()` to check the values directly. Be aware NaN is also of type number – Reyno Dec 18 '19 at 13:27
1

You can use a reduce function

function getNumbersObj(obj) {
  return Object.keys(obj).reduce((newObj, key) => {
    const value = obj[key];
    if (typeof value === 'number') {
      newObj[key] = value
    }
    return newObj
  }, {})
}

Edit: for date_created which is a number, you'd could explicitly filter out the key

function getNumbersObj(obj) {
  return Object.keys(obj).reduce((newObj, key) => {
    const value = obj[key];
    if (typeof value === 'number' && key !== 'date_created') {
      newObj[key] = value
    }
    return newObj
  }, {})
}
Jeffrey Devloo
  • 1,406
  • 1
  • 11
  • 20
1

You can use loop through with filter and return only numbers

 const objects = {
    date_created: 1546293600000,
    num: 370,
    num2: NaN,
    num3: 100,
    category: 'C'
    }

    let newObj = Object.keys(objects).filter( key => 
    !isNaN(key)) 
    
    console.log(newObj)
Ericgit
  • 6,089
  • 2
  • 42
  • 53
Suraj Auwal
  • 322
  • 3
  • 9
1

Not the best solution from efficiency point of view but here is a solution:

const originalObj = {
  date_created: 1546293600000,
  num: 370,
  num2: NaN,
  num3: 100,
  category: "C"
}

const objWithUndefined ={
  date_created: 1546293600000,
  num: 370,
  num2: NaN,
  num3: undefined,
  category: "C"
}
const objWithNull ={
  date_created: 1546293600000,
  num: null,
  num2: NaN,
  num3: 100,
  category: "C"
}

function modifyObject(original) {
  let newObj = Object.assign({}, original);

  delete newObj.date_created;
  delete newObj.num2;
  delete newObj.category;
  
  for (var prop in newObj) {
    if (newObj[prop] == undefined || newObj[prop] == null) {
      delete newObj[prop]

    }
  }
  return newObj;
}

console.log("original: " + JSON.stringify(modifyObject(originalObj), null, 2));
console.log("objWithUndefined: " + JSON.stringify(modifyObject(objWithUndefined), null, 2));
console.log("objWithNull: " + JSON.stringify(modifyObject(objWithNull), null, 2));
Mara Black
  • 1,666
  • 18
  • 23