There is a loop in the js click event. Can I click to execute the loop?

let arr = [

]
                {id:1,text:""},
                {id:2,text:""},
                {id:3,text:""}
                ];

let items = [];
$("- sharpadd") .click (function () {
for (var i = 0polii

    items.push(arr[i]); 
 }  

})
/ / if written in this way, all the data of arr will be added directly. Hopefully, one click to add an id in arr

Jun.07,2021

you can first determine the id, and then make a comparison
var id =''

$("-sharpadd").click(function(){
    for(var i = 0;i<arr.length;iPP){
        if (id === arr[i].id) {
            items.push(arr[i]); 
        }
    }  
})

closure + immediately execute function implementation:

$('-sharpadd').click(
    (function() {
        let count = 0;
        return function() {
            items.push(arr[countPP]);
        };
    })()
);

  // indexid
  let index = 0;
  let arr = [
    {id:1,text:""},
    {id:2,text:""},
    {id:3,text:""}
  ];
  let arrLength = arr.length;
  let items = [];
  document.getElementById("add").onclick = function(){
    // 
    if (index <= arrLength - 1) {
      items.push(arr[index]);
      // 
      indexPP;
    }
    console.log(items);
  }
Menu