-2

I am working on react native project.

I am getting response from server as follows.

[
        {
            "id": 58,
            "year": 2000,
            "value": "1450",
        },  {
            "id": 58,
            "year": 2001,
            "value": "1,980",
        },
         {
            "id": 58,
            "year": 2014,
            "value": "600",
        },  {
            "id": 58,
            "year": 2015,
            "value": "200",
        },  {
            "id": 58,
            "year": 2016,
            "value": "180",
        },  {
            "id": 58,
            "year": 2017,
            "value": "145",
        },  {
            "id": 58,
            "year": 2018,
            "value": "1650",
        },  {
            "id": 58,
            "year": 2019,
            "value": "1444",
        },

]

But, I have to take only last 5 years data only.

How to filter this in react native?

  • `"value": “1120”,` will throw a `SyntaxError`. Best to fix whatever sending you the data so that it can be parsed properly – CertainPerformance Mar 21 '19 at 08:11
  • 1
    Please visit the [help], take the [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output. – mplungjan Mar 21 '19 at 08:11
  • This is purely a JavaScript problem, did you search for now to filter an array? Or maybe just how to take the last n elements, if it's ordered? – jonrsharpe Mar 21 '19 at 08:12
  • This is how trivial it was: `const last5 = new Date().getFullYear()-5; console.log(data.filter((d) => d.year >= last5))` – mplungjan Mar 21 '19 at 08:21
  • OR `console.log(data.sort((a,b)=>b.year-a.year).slice(0,6))` – mplungjan Mar 21 '19 at 08:24
  • I am new to react native, That is why I have raised query here. Thank you – Anilkumar iOS - ReactNative Mar 21 '19 at 08:25
  • I would like to show last 5 years (latest years) data, not like 5 years (like 2001, 2005, etc), I have to show only last 5 years latest years like (2014,2015,2016, etc till current year) – Anilkumar iOS - ReactNative Mar 21 '19 at 10:17
  • I found solution finally, I just ascending the year values. Then I can fetch last 5 indexes of data from array. const lastFiveYearsData = ReturnsData.sort((a, b) => a.year - b.year); – Anilkumar iOS - ReactNative Mar 21 '19 at 10:57

1 Answers1

0

Use array filter and use new Date().getFullYear() to get the current year and subtract the year in object and check if it is greater than 5

let data = [{
    "id": 58,
    "year": 2000,
    "value": "1120",
  }, {
    "id": 58,
    "year": 2001,
    "value": "1,980",
  },
  {
    "id": 58,
    "year": 2014,
    "value": "600",
  }, {
    "id": 58,
    "year": 2015,
    "value": "200",
  }, {
    "id": 58,
    "year": 2016,
    "value": "180",
  }, {
    "id": 58,
    "year": 2017,
    "value": "145",
  }, {
    "id": 58,
    "year": 2018,
    "value": "1650",
  }, {
    "id": 58,
    "year": 2019,
    "value": "1444",
  }
]

let lastFiveYrs = data.filter(function(item) {
  return new Date().getFullYear() - item.year >= 5;
});

console.log(lastFiveYrs)
brk
  • 48,835
  • 10
  • 56
  • 78