Given the following array:
arrOfOranges =
[{ "kerp": "thisThing", "time": "@ rocks 3"},
{ "kerp": "otherThing", "green": "blah"},
{ "kerp": "thisThing", "time": "(countriesToTheStart ^"},
{ "kerp": "anotherThing", "yellow": "row row"},
{ "kerp": "anotherThing", "yellow": "rigorous"},
{ "kerp": "thisThing", "time": ",,,,"}]
Each item in the array has the fixed structure:
[{kerp: ''}] that can be one of three values: thisThing, otherThing, or anotherThing. The second key for each object depends on the value of 'kerp'.
If kerp is 'thisThing', then the second key is 'time'. => { "kerp": "thisThing", "time": ""},
If kerp is 'otherThing', then the second key is 'green'. => { "kerp": "otherThing", "green": ""},
If kerp is 'anotherThing', then the second key is 'yellow'. => { "kerp": "anotherThing", "yellow": ""},
I am trying to extract the last item in the array with 'kerp': 'thisThing'. However, the value associated with the 'time' key must contain a letter.
In the given array, the last item has 'kerp': 'thisThing', but the 'time' does not contain a letter.
Therefore, I need to retrieve the item from arrOfOranges where it's the last item with kerp as thisThing and time containing letters in the value:
{"kerp": "thisThing", "time": "(countriesToTheStart ^"}
Here is my current approach:
if (arrOfOranges.filter(a=>a.kerp === "thisThing")) {
// Checking if the time value contains any letters
if(arrOfOranges.time.match(/[\w]+/g)) {
return arrOfOranges.slice(-1)[0];
}
}
However, this solution is not working as intended. How can I achieve the desired outcome?