Cloud Stack Ninja

my problem is that I have an array named object of several objects which are themselves in an array. How to retrieve them with the splice and findindex method? For the moment with my current code it removes the last object from the array when I click on any which button deleted from the basket. I have another object2 which is a simple array containing only the id's and the method to use works very well . I want to delete an object from the object array by searching by the id. With exemple object2 array ,the method works very well in my code, but not with the object array please help me for this.

object :

(5) [Array(2), Array(2), Array(2), Array(2), Array(2)]
0: Array(2)
0: {lenses: Array(2), _id: "5be1ed3f1c9d44000030b061", name: "Zurss 50S", price: 49900, description: "Lorem ipsum dolor sit amet, consectetur adipisicin…mpor incididunt ut labore et dolore magna aliqua.", …}
1: "35mm 1.4"
length: 2
__proto__: Array(0)
1: (2) [{…}, "50mm 1.8"]
2: (2) [{…}, "25mm 4.5"]
3: (2) [{…}, "50mm 1.7"]
4: (2) [{…}, "50mm 1.4"]
length: 5
__proto__: Array(0)

object2 :

(5) ["5be1ed3f1c9d44000030b061", "5be1ef211c9d44000030b062", "5be9bc241c9d440000a730e7", "5be9c4471c9d440000a730e8", "5be9c4c71c9d440000a730e9"]

here is my code

butonAll[i].addEventListener("click", function (e) {
        console.log("supprime moi");
        const id = e.target.getAttribute("data-id");

        objectJs2.splice(
          objectJs2.findIndex((x) => {
            return x === id;
          }),
          1
        );
        objectJs.splice(
          objectJs.findIndex((x) => {
            return x === id;
          }),
          1
        );
        localStorage.setItem("object", JSON.stringify(objectJs));
        localStorage.setItem("id", JSON.stringify(objectJs2));
        console.log(id);
        console.log(objectJs.length);
        console.log(line2);
        //location.href = "panier.html";
        if (objectJs.length === 0) {
          localStorage.removeItem("object");
          localStorage.removeItem("id");
        }
      });


Read more here: https://stackoverflow.com/questions/64400923/method-splice-et-findindex-javascript-problem-search-board

Content Attribution

This content was originally published by gerald frron at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: