0

I need to get the values ​​of the parameters of a JSON model I have.

My output returns:

console.log(this.model);

{ year:2017, month:08, day:05}

I need to get the values and create a object for example date='2017-08-05'.

chooban
  • 9,018
  • 2
  • 20
  • 36
  • That format that you put the json makes no sense, there's no way to a json return a object like `{ year=2017 }`. It is a string? Like `{ "year=2017" }`? Or maybe the output returns `{ year : 2017 }`? – Yago Azedias Aug 15 '17 at 02:05
  • sorry, you have a reason, my json { year:2017, month:08, day:05} – Lenin Padilla Aug 15 '17 at 02:13

1 Answers1

2

Depending on what you need to do with it, you could use a JavaScript template literal: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

myNewObject.date = `${this.model.year}-${this.model.month}-${this.model.day}`;

If the date values are really numbers and not strings, then you may need to "0" pad them. There is an example for this here: How to output integers with leading zeros in JavaScript

DeborahK
  • 57,520
  • 12
  • 104
  • 129
  • Its will return `2017-8-5` I hope that it does not need to have the 0 before, like `2017-08-05` – Yago Azedias Aug 15 '17 at 02:22
  • That is, date = '2017-08-05' and go inside object = {date: '2017-08-05', name: "lenin", token : "5646d54adad"} but the object to which I send I am shown this object = {0: "2", 1: "0",3:"1",4:"7",name: "lenin", token :"5646d54adad"} – Lenin Padilla Aug 15 '17 at 02:31