0

I have an object like below. Sometimes it will contain status sometimes not. I want to convert status value format into Number when it in object.

var obj={ 
  name: 'abc',
  status: '1',
  updated_time: { 
    '$gt': 2019-11-03T00:00:00.000Z,
    '$lt': 2019-11-03T15:23:55.838Z 
  } 
}

i've tried as below but it's not converted;

if(obj.status){
  parseInt(obj.status)
}
console.log("$$$$$$$$$$$$$$$$",obj)

console.log print

$$$$$$$$$$$$$$$${ name: 'abc', status: '1', updated_time: { '$gt': 2019-11-03T00:00:00.000Z, '$lt': 2019-11-03T15:23:55.838Z } }

1
  • 3
    parseInt returns the converted value. You are not storing it anywhere. Commented Nov 3, 2019 at 10:16

3 Answers 3

1

You need to assign back parsed value to object.status. parseInt method just evaluate the value it doesn't changes value by itself in object. you need to change value manually on object.

var obj = {
  name: 'abc',
  status: '1',
  updated_time: {
    '$gt': '2019-11-03T00:00:00.000Z',
    '$lt': '2019-11-03T15:23:55.838Z'
  }
}

if (obj.status) {
  obj.status = parseInt(obj.status)
}
console.log("$$$$$$$$$$$$$$$$", obj)

Sign up to request clarification or add additional context in comments.

Comments

1

This is just a simple mistake mate. You haven't assigned the value.

You can do that by :

if(obj.status){
  obj.status = parseInt(obj.status); //obj.status is being reassigned
}

Hope it helps :)

Comments

1

parseInt returns a value and you need to assign this value to object.status property:

if(obj.status){
  obj.status = parseInt(obj.status)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.