Filtering the array of objects in the Vuex state resulted in an empty array being

I am working with a state that fetches data from

https://jsonplaceholder.typicode.com/todos/
. My goal is to filter this data based on the status completed:true or completed:false.

Below is the method I am using to filter the array:

filterByStatus(status) {
  const filteredResults = this.allTodos.filter(todo => todo.completed == status)
  console.log(filteredResults)
}

When I pass true or false as a parameter to the method, console.log(filteredResults) is returning an empty array.

filterByStatus(status) {
  let filteredResults = [];
  this.allTodos.map(item => {
    if (item.completed == status) {
      filteredResults.push(item);
    }
  });
  console.log(filteredResults);
}

I also attempted to use the map method, but the result remains an empty array.

When I console.log(this.allTodos), the result is:

 [{…}, {…}, {…}, {…}, {…}, __ob__: Observer]

Could this be due to the Observer?

Answer №1

It is advisable not to use map in this scenario:

        this.allTodos.map(item => {
          if (item.completed == status) {
             filteredResults.push(item);
          }
        });

Instead, consider using forEach:

        this.allTodos.forEach(item => {
          if (item.completed == status) {
             filteredResults.push(item);
          }
        });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring elements with Watir WebDriver and ExtJS

Currently, I am performing an acceptance test using watir-webdriver with Ruby. I have a question regarding ExtJs support with Watir webdriver. Is it possible to locate elements that are dynamically generated by ExtJS? I have attempted the following: @brow ...

Using node-native to authenticate in MongoDB is a surefire way to ensure the

I'm currently facing an issue while attempting to save a document in MongoDB within my Nodejitsu/MongoHQ application. Everything works perfectly locally, but the MongoHQ database requires authentication and it fails even with the correct user/password ...

Pause for a moment before commencing a fresh loop in the FOR loop in JavaScript

Behold, I present to you what I have: CODE In a moment of curiosity, I embarked on creating a script that rearranges numbers in an array in every conceivable way. The initial method I am working with is the "Selection mode", where the lowest value in th ...

Access another key within an object

Here's a code snippet from my module: exports.yeah = { hallo: { shine: "was", yum: this.hallo.shine } } In the above code, I'm attempting to reference the shine property in yum: this.hallo.shine However, when I run the script, ...

I am looking for a way to write a function that will emit an event to the parent component when an icon is

I am creating an input element with an icon to show/hide the password. How can I emit an event in the v-icon to notify the parent component when the icon is clicked? Here is my child component BaseInputPassword: <div class="label"> ...

Opening the identical document

In my coding scenario, I am programmatically writing to a text file using Java while simultaneously attempting to read from the same file using jQuery. Unfortunately, I am encountering an issue where jQuery is unable to detect the updated content whenever ...

The ajax function is malfunctioning when called from an external JavaScript file

I am having an issue with a Registration page that only has UserName and Password fields. When I click on the Submit button, I want to be able to submit the new User Details using an ajax call with jQuery. I have tried defining an Insert function on butt ...

What is the best way to apply custom styles in reactJs to toggle the visibility of Google Maps?

There are three radio buttons that correspond to school, restaurant, and store. Clicking on each button should display nearby locations of the selected type. Displaying Google Map and nearby places individually works fine without any issues. class Propert ...

Google Scripts: Generating a set of data to include in an email

As a newcomer to Google Script and JavaScript, I'm on a mission to email a list of file names extracted from a spreadsheet. The names reside in a column within my sheet, and after defining a variable called "newfiles" to cherry-pick only the necessary ...

Issue with bootstrap 4 CDN not functioning on Windows 7 operating system

No matter what I do, the CDN for Bootstrap 4 just won't cooperate with Windows 7. Oddly enough, it works perfectly fine on Windows 8. Here is the CDN link that I'm using: <!doctype html> <html lang="en> <head> <!-- Req ...

Is it normal for Tailwind animation to loop twice when transitioning between pages in Next.js?

I'm currently utilizing react-hot-toast for displaying alerts and animating them during page transitions. The animation involves a double fade-in effect when transitioning between pages. In my project, I've integrated tailwindcss-animate within ...

Issue with exporting VueJS-generated HTML containing checkboxes results in loss of checkbox checked state

In the template of my component, I have a checkbox code that looks like this: <div ref="htmlData"> <input type="checkbox" class="mycb" :id="uniqID" :disabled="disabled" v-model="cbvalue" > &l ...

What is causing my basic angularjs to not function properly?

In my angularjs test instance, I am using a global variable but the alert tag is not functioning as expected. This code snippet is from my first example on codeschool. Any thoughts on why the alert is not running? <!DOCTYPE html> <html xmlns="ht ...

Deriving variable function parameters as object or tuple type in TypeScript

Searching for a similar type structure: type ArgsType<F extends Function> = ... which translates to ArgsType<(n: number, s: string)=>void> will result in [number, string] or {n: number, s: string} Following one of the provided solu ...

Selenium htmlUnit with dynamic content failing to render properly

My current project involves creating Selenium tests for a particular website. Essentially, when a user navigates to the site, a CMS injects some dynamic elements (HTML + JS) onto the page. Everything works fine when running tests on the Firefox driver. H ...

Contrasting actions observed when employing drag functionality with arrays of numbers versus arrays of objects

Being a newcomer to D3 and JavaScript, I'm hoping someone can help me clarify this simple point. I am creating a scatter graph with draggable points using code that closely resembles the solution provided in this Stack Overflow question. When I const ...

The RangeError occurs when attempting to deploy to Heroku due to exceeding the maximum call stack size at Array.map in an anonymous function

While attempting to deploy a MERN stack application to Heroku, I encountered an error in the Heroku CLI. RangeError: /tmp/build_c861a30c/frontend/node_modules/@reduxjs/toolkit/dist/redux-toolkit.esm.js: Maximum call stack size exceeded at Array. ...

What is the best way to create a dynamic data structure using an array?

I recently discovered that Vuejs does not track changes in data beyond the first level of an array. Is there a way to modify this behavior? new Vue({ el: '#container', data: { value: [], }, beforeMount() { this.value[0] = &apo ...

Three fixed position divs arranged horizontally side by side

I am attempting to organize 3 divs in a row using Flex. ISSUE 1: The div that is centered is set with position: fixed. However, the two other divs on each side do not stay aligned with the centered fixed div when scrolling. If I change the centered div to ...

Searching for specific items within an array of objects using Mongoose

Currently, I am working with a Mongoose schema that looks like this: var MessageSchema = new Schema({ streamer: { streamer_username: String, streams: [{ id: String, messages: [{ date: String, ...