1

I have array like this:

var myMovie = [];

And object like this:

var first = {id:"tt3783958", title:"La La Land", year:"2016", type:"Comedy, Drama, Music"}

I added object into local storage array via OnClick() on button.

$(document).ready(function() {
   $(".btn.btn-outline-success").click(function () {

    myMovie.push(first); 

    localStorage.setItem('myMovie', JSON.stringify(myMovie));
    var output = JSON.parse(localStorage.getItem('myMovie'));

After adding object into local storage, local storage looks like this:

[{id:"tt3783958", title:"La La Land", year:"2016", type:"Comedy, Drama, Music"}]

My question here is, how could I remove whole object via variable name? I want to remove object first from myMovie. Index position of object may be different each time.

[{id:"tt3783958", title:"La La Land", year:"2016", type:"Comedy, Drama, Music"}, [{id:"tt9574821", title:"Ghost Protocol", year:"2012", type:"Action, Adventure"}]
6
  • This is a different example, and I also tried your solution, but it doesn't apply to my code. Commented Jun 4, 2017 at 10:52
  • nope. The code stays the same. Commented Jun 4, 2017 at 11:07
  • Problem whit your previous code is that nothing happens, I just get some console errors. Could you show me example for this code? Commented Jun 4, 2017 at 11:15
  • you want to convert first to index?? You mean if the variable name is second, you want to remove item at second index?? Commented Jun 4, 2017 at 11:27
  • I want to remove this object {id:"tt3783958", title:"La La Land", year:"2016", type:"Comedy, Drama, Music"} from array myMovie even if is on second index or third in array. Commented Jun 4, 2017 at 11:31

3 Answers 3

2

Best would be a function, that not only changes the array but also stores it in localStorage.

To find the right index to remove, you could find the object with the right title:

    var myMovie=[] //JSON.parse(localStorage.getItem('myMovie')||"[]");
    
    function push(el){
    myMovie.push(el);
    //localStorage.setItem('myMovie', JSON.stringify(myMovie));
    }

    function remove(title){
     var i=myMovie.findIndex(movie=>movie.title===title);
     if(i!==-1){
       myMovie.splice(i,1);
       //localStorage.setItem('myMovie', JSON.stringify(myMovie));
     }
    }
    
    push({id:"tt3783958", title:"La La Land", year:"2016", type:"Comedy, Drama, Music"});
    console.log(myMovie);
    remove("La La Land");
    console.log(myMovie);

To make a running stackSnippet, i had to remove the localStorage functionality, it is commented out.

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

Comments

1

I would advice you to have your object in format

var first = {"tt3783958": { title:"La La Land", year:"2016", type:"Comedy, Drama, Music"}}

ID will make your object have unique keys. Update will also be easy.

And store these in a JSON object and not an array like

myMovie = {}; //note its an object

This would make it easier for you to query and delete items.

Comments

0

you need to get the object and set new one:

myMovie = JSON.parse(localStorage.getItem("myMovie"));
myMovie.splice(0,1);
localStorage.setItem("myMovie",JSON.stringify(myMovie));

1 Comment

This seems to create new empty 'myMovie' array. And its set to delete first index instead of deleting via variable name 'first'.

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.