0

My list:

var list = [{"id": 1, "date": 1649368800000}, {"id": 2, "date": 1649368800000}, {"id": 3, "date": 1649368800000}]

Desired output:

var list = [{"id": 1, "date": 01.01.2022}, {"id": 2, "date": 1649368800000}, {"id": 3, "date": 01.01.2022}]

How can I effectively iterate through my list and parse my int values in "date" to a human readable date format?

For the start I tried to just print the date values..

var list_of_values = [a_dict["date"] for a_dict in list]

but failed with Parsing error: ',' expected

JohnDole
  • 525
  • 5
  • 16

1 Answers1

2

I think you're confusing with .

That short-for loop is the python way


Since you tagged this , I'd recommend forEach() and new Date():


Parsed to date format

var list = [{"id": 1, "date": 1649368800000}, {"id": 2, "date": 1649368800000}, {"id": 3, "date": 1649368800000}];

list.forEach(o => o.date = new Date(o.date));

console.log(list);
Parsed to dd.mm.yyyy

var list = [{"id": 1, "date": 1649368800000}, {"id": 2, "date": 1649368800000}, {"id": 3, "date": 1649368800000}];

list.forEach(o => {
  const d = new Date(o.date);
  o.date = `${d.getDate()}.${d.getMonth() + 1}.${d.getFullYear()}`
});

console.log(list);
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • Hi 0stone0, thank you! You are correct. I mixed it up. Thank you for extracting the dates. How can I manipulate my original date with your .map function, so that the int values are replaced with the date values? – JohnDole Apr 04 '22 at 09:03
  • What do you mean by 'date values'? Please click the link of the `new Date`, you can add a format like you want. – 0stone0 Apr 04 '22 at 09:05
  • Sorry, I mean: With your code, I receive the list of the parsed dates. But my original data "list" is still untouched. I want to replace the values inside "list" and not extract the dates – JohnDole Apr 04 '22 at 09:12
  • Ahh, so you want `list` to become an array with the 3 unix timestamps? – 0stone0 Apr 04 '22 at 09:14
  • Yes. The original list (array of dictionaries) should remain the same with the exception of the parsed dates! :) (Thank you for your time btw) – JohnDole Apr 04 '22 at 09:15
  • please see my edit, i think you want something like this; i've used `foreach` instead off map since you don't needd a single return – 0stone0 Apr 04 '22 at 09:16
  • Hi again, I think this piece of code will convert my list of dictionaries to a list with only the converted date and nothing else. My console output is like [Fri Apr 08 2022 00:00:00 GMT+0200 (Mitteleuropäische Sommerzeit),Fri Apr 08 2022 00:00:00 GMT+0200 (Mitteleuropäische Sommerzeit),Fri Apr 08 2022 00:00:00 GMT+0200 (Mitteleuropäische Sommerzeit),Fri Apr 08 2022 00:00:00 GMT+0200 (Mitteleuropäische Sommerzeit),Fri Apr 08 2022 00:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)] now, without the single dictionaries – JohnDole Apr 04 '22 at 09:20
  • Yea I was to fast, did you see my latest edit with `foreach`? – 0stone0 Apr 04 '22 at 09:21
  • I have added my desired output in my thread, if its still unclear – JohnDole Apr 04 '22 at 09:21