1

Im trying to filter my array of objects by the current month. Using animal crossing fish as an example

const fishData = {
   "fish_name": "Barreleye",
   "price": "15,000",
   "location": "Sea",
   "shadow_size": "Small",
   "n_March": true,
   "n_3": true,

 },
 {
   "fish_name": "Coelacanth",
   "price": "15,000",
   "location": "Sea (Rainy Days)",
   "shadow_size": "Largest",
   "n_3": true,

 }
]

var today = new Date();
var currentMonth = today.getMonth();

var fishMonth = `n_ + ${currentMonth}`;
console.log(fishMonth);
var filteredFish = fishData.filter(function(i) {
    return i.fishMonth == true;
});

now by return if i put "n_3" instead of "fishMonth" the code runs fine. I've checked the "fishMonth" and it does return n_3. What would be stoping this from working?

2 Answers 2

1

There are unnecessay characeter in your fishMonth variable, it should be:

var fishMonth = `n_${currentMonth}`;

and you also want to read the object's key so there has to be return i[fishMonth] == true;, try:

const fishData = [{
   "fish_name": "Barreleye",
   "price": "15,000",
   "location": "Sea",
   "shadow_size": "Small",
   "n_March": true,
   "n_3": true,

 },
 {
   "fish_name": "Coelacanth",
   "price": "15,000",
   "location": "Sea (Rainy Days)",
   "shadow_size": "Largest",
   "n_3": true,

 }
]

var today = new Date();
var currentMonth = today.getMonth();

var fishMonth = `n_${currentMonth}`;
var filteredFish = fishData.filter(function(i) {
    return i[fishMonth] == true;
});
console.log(filteredFish);

Sign up to request clarification or add additional context in comments.

Comments

0

You need the right key value without space and + and the right property accessor with brackets.

You could take some more changes, for example get the month directly from the instance and return directly the value of the wanted property.

const
    fishData = [{ fish_name: "Barreleye", price: "15,000", location: "Sea", shadow_size: "Small", n_March: true, n_3: true }, { fish_name: "Coelacanth", price: "15,000", location: "Sea (Rainy Days)", shadow_size: "Largest", n_3: true }],
    fishMonth = `n_${(new Date).getMonth()}`,
    filteredFish = fishData.filter(fish => fish[fishMonth]);

console.log(filteredFish);

Finally, you could change the whole data structure and add the month as value to the objects and use something like a month property. This allowes to use a simple comparison with the value instead of using a compound key.

const
    fishData = [{ fish_name: "Barreleye", price: "15,000", location: "Sea", shadow_size: "Small", n_March: true, month: 3 }, { fish_name: "Coelacanth", price: "15,000", location: "Sea (Rainy Days)", shadow_size: "Largest", month: 3 }],
    fishMonth = (new Date).getMonth(),
    filteredFish = fishData.filter(({ month }) => month === fishMonth);

console.log(filteredFish);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.