1

Supposed I have a response of

enter image description here

and I'm looping it via and push the pic.id on files array

files = []

response.data[0].pics.forEach(function(pic, i){
      let div = makeButtons(response.data[0].pics, files, imgPrevEdit);
      files.push(pic.id);
});

so after pushing the file array will become like this

enter image description here

the make button function is use to delete that certain element on the array using splice function

makeButtons: function(file, store, elem) {
    let button = document.createElement("button");
    button.classList.add('position-absolute','x', 'rounded-circle');
    button.type = "button";
    button.innerHTML= "x";

    button.addEventListener('click', () => {
        elem.removeChild(div);
        store.splice(store.indexOf(file), 1);
    });

    let div = document.createElement("div");
    div.classList.add('col', 'mt-2');
    div.appendChild(button);
    return div;
},

However when i click the button it always start at the end of files array it doesnt find the specific key and delete it

any idea how can i solve it?

0

1 Answer 1

1

it always start at the end of files array

I think it happens because store.indexOf(file) returns -1. In this case splice

will begin that many elements from the end of the array (with origin -1, meaning -n is the index of the nth last element and is therefore equivalent to the index of array.length - n)

Why store.indexOf(file) returns -1? Because you pass response.data[0].pics as file argument, but you actually push pic.id to files which you pass as store:

let div = makeButtons(response.data[0].pics, files, imgPrevEdit);
files.push(pic.id);
Sign up to request clarification or add additional context in comments.

2 Comments

what should i pass as file argument? im confused
@Beginner try to pass pic.id

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.