Question

I delete elements stored in simple-storage array in main script.This creates null elements in the simple-storage array.So i run a script to return a new array to the main addon script (which contains no null elements) and i assign it to simple-storage array.But the main script gets only one element not the entire array. How to get the entire array? below is the main addon code part:

text_entry.port.on("del", function (todel) {
  console.log(todel);
  site_l=ss.storage.sites.length;

  for(var i=0;i<site_l;i++)
  {
    if(ss.storage.sites[i]==todel)
    {
        delete ss.storage.sites[i];
    }   
  }
  text_entry.port.emit("c",ss.storage.sites);
  text_entry.port.on("cd",function(arr){
    ss.storage.sites=[];
    ss.storage.sites=arr;
  });

});

script which returns new array containing no null elements:

self.port.on("c",function(arr)
{
    var a=arr;
    var l=a.length;

    function isEmpty(element) {
    if(element!=null)
      return true;
    }

    a=a.filter(isEmpty);
    self.port.emit("cd",a);
});
Was it helpful?

Solution

Replace

for(var i=0;i<site_l;i++) {
  if(ss.storage.sites[i]==todel) {
    delete ss.storage.sites[i];
  }   
}

With ss.storage.sites.splice(ss.storage.sites.indexOf(todel), 1)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top