JavaScript Vue computed property changes the data object

I have basically this structure for my data (this.terms):

{
    name: 'First Category',
    posts: [
        {
            name: 'Jim James',
            tags: [
                'nice', 'friendly'
            ]
        },
        {
            name: 'Bob Ross',
            tags: [
                'nice', 'talkative'
            ]
        }
    ]
},
{
    name: 'Second Category',
    posts: [
        {
            name: 'Snake Pliskin',
            tags: [
                'mean', 'hungry'
            ] 
        },
        {
            name: 'Hugo Weaving',
            tags: [
                'mean', 'angry'
            ]
        }
    ]
}

I then output computed results so people can filter this.terms by tags.

computed: {
    filteredTerms: function() {
        let self = this;
        let terms = this.terms; // copy original data to new var

        if(this.search.tags) {
            return terms.filter((term) => {
                let updated_term = {}; // copy term to new empty object: This doesn't actually help or fix the problem, but I left it here to show what I've tried.
                updated_term = term;

                let updated_posts = term.posts.filter((post) => {
                    if (post.tags.includes(self.search.tags)) {
                        return post;
                    }
                });

                if (updated_posts.length) {
                    updated_term.posts = updated_posts; // now this.terms is changed even though I'm filtering a copy of it
                    return updated_term;
                }
            });
        } else {
            return this.terms; // should return the original, unmanipulated data
        }
    }
},

filteredTerms() returns categories with only the matching posts inside it. So a search for "angry" returns just "Second Category" with just "Hugo Weaving" listed.

The problem is, running the computed function changes Second Category in this.terms instead of just in the copy of it (terms) in that function. It no longer contains Snake Pliskin. I've narrowed it down to updated_term.posts = updated_posts. That line seems to also change this.terms. The only thing that I can do is reset the entire data object and start over. This is less than ideal, because it would be loading stuff all the time. I need this.terms to load initially, and remain untouched so I can revert to it after someone clears their search criterea.

I've tried using lodash versions of filter and includes (though I didn't really expect that to make a difference). I've tried using a more complicated way with for loops and .push() instead of filters.

What am I missing? Thanks for taking the time to look at this.

Answer:1

Try to clone the object not to reference it, you should do something like :

   let terms = [];
   Object.assign(terms,this.terms);
Answer:2

let terms = this.terms;

This does not copy an array, it just holds a reference to this.terms. The reason is because JS objects and arrays are reference types. This is a helpful video: https://www.youtube.com/watch?v=9ooYYRLdg_g

Anyways, copy the array using this.terms.slice(). If it's an object, you can use {...this.terms}.

Answer:3

During development of a vue project, and got some doubt regarding template / render / VNode. After reading the document https://vuejs.org/v2/guide/syntax.html, and google search, still didn't ...

During development of a vue project, and got some doubt regarding template / render / VNode. After reading the document https://vuejs.org/v2/guide/syntax.html, and google search, still didn't ...

  1. what is a relationship template

I have the following object: productDetails: { cislife: [], prime: [] } I want to return null instead of empty array if there aren't any values available. e.g: I want to check if any of ...

I have the following object: productDetails: { cislife: [], prime: [] } I want to return null instead of empty array if there aren't any values available. e.g: I want to check if any of ...

  1. loop through object javascript
  2. loop through object keys javascript
  3. loop through object properties
  4. loop through object typescript
  5. loop through object keys
  6. loop through object properties c#
  7. loop through object c#
  8. loop through object array javascript
  9. loop through object react
  10. loop through object es6
  11. loop through object php
  12. loop through object java
  13. loop through object lodash
  14. loop through object angular
  15. loop through object jquery
  16. loop through object python
  17. loop through object javascript es5
  18. loop through object javascript key value
  19. loop through object values
  20. loop through object jsx

So I'm trying to make a function that grabs a HTML element and then after I've selected the HTML element I try to apply CSS to it via JavaScript but it doesn't seem to work. Instead, I get an error ...

So I'm trying to make a function that grabs a HTML element and then after I've selected the HTML element I try to apply CSS to it via JavaScript but it doesn't seem to work. Instead, I get an error ...

  1. javascript function that returns a function
  2. javascript function that returns a value
  3. javascript function that returns a promise
  4. javascript function that does nothing
  5. javascript function that returns boolean
  6. javascript function that accepts array
  7. javascript function that returns array
  8. javascript function that returns an object
  9. javascript function that can be suspended and resumed
  10. javascript function that adds numbers
  11. javascript function that returns nothing
  12. javascript function that only runs once
  13. javascript function that calls another function
  14. javascript function that runs on page load
  15. javascript function that executes immediately
  16. javascript function that takes a string
  17. javascript function that adds two numbers
  18. javascript function that returns html
  19. javascript function that takes an array
  20. javascript function that always runs

I tried to load a page inside my phonegape aplication with Jquery .load() but it doesn't work since it's not on a server and it's on local machine. When I'll upload the app on build. phonegape my ...

I tried to load a page inside my phonegape aplication with Jquery .load() but it doesn't work since it's not on a server and it's on local machine. When I'll upload the app on build. phonegape my ...

  1. phonegap load local html file
  2. phonegap load local file