arrays. I tried sorting out items in array according to date and mapping the data but I'm facing issues. Any assistance?
Code??
Your question is lacking. Please include: - What you have - What you want - What you have tried - The results of what you tried - Any error messages For example: "I have the string "52 854". I want it as the number 52854. I tried using Number("52 854"), but it just becomes NaN. I also tried Number.parseInt("52 854"), but it became just 52. What am I doing wrong?" For a more detailed guide, see StackOverflow's guide to asking
generally, nestedArr.sort(subArr => Math.max(...subArr.map(_ => _.date)))
something like nestedArr.sort((a, b) => Math.max(...a.map(_ => _.date)) > Math.max(...b.map(_ => _.date)) ? -1 : 1)
{ "products": [ { "id": 1, "name": "Product 1", "prices": [ { "id": 1, "price": 46, "date": "2019-01-01T17:16:32+00:00" }, { "id": 2, "price": 34, "date": "2018-11-01T17:16:32+00:00" } ] }, { "id": 2, "name": "Product 2", "prices": [ { "id": 3, "price": 45, "date": "2019-01-01T17:16:32+00:00" }, { "id": 4, "price": 56, "date": "2018-11-01T17:16:32+00:00" } ] }, { "id": 3, "name": "Product 3", "prices": [ { "id": 5, "price": 56, "date": "2017-01-01T17:16:32+00:00" }, { "id": 6, "price": 46, "date": "2018-11-01T17:16:32+00:00" } ] } ] }
{ "products": [ { "id": 1, "name": "Product 1", "prices": [ { "id": 1, "price": 46, "date": "2019-01-01T17:16:32+00:00" }, { "id": 2, "price": 34, "date": "2018-11-01T17:16:32+00:00" } ] }, { "id": 2, "name": "Product 2", "prices": [ { "id": 3, "price": 45, "date": "2019-01-01T17:16:32+00:00" }, { "id": 4, "price": 56, "date": "2018-11-01T17:16:32+00:00" } ] }, { "id": 3, "name": "Product 3", "prices": [ { "id": 5, "price": 56, "date": "2017-01-01T17:16:32+00:00" }, { "id": 6, "price": 46, "date": "2018-11-01T17:16:32+00:00" } ] } ] } So I have this array of products and I have mapped the data, but I want the key prices to return the sorted array of prices in order of recent date
Hope you get it now @Suppen
didn't work for me
Обсуждают сегодня