-1

I have a nested array in localStorage

arr = [["STAR_SPORTS_2-20170924-200043-210917-00142.jpg", "TShirt", "Redshift", 267, 403, 377, 389, 385, 479, 275, 493], 
       ["STAR_SPORTS_2-20170924-200043-210917-00142.jpg", "TShirt", "Redshift", 317, 319, 428, 325, 426, 372, 315, 366],
       ["STAR_SPORTS_2-20170924-200043-210917-00142.jpg", "TShirt", "Redshift", 425, 490, 539, 425, 526, 472, 215, 478]]

Based on two point condition, I want to delete a specific array and store it back to the localStorage.

var a = 267;
var b = 403;

for (var i = 0; i < arr.length; i++) {
    if (arr[i][0] === "STAR_SPORTS_2-20170924-200043-210917-00142.jpg") {
      first.x = arr[i][3];
      first.y = arr[i][4]; 
      second.x = arr[i][5]; 
      second.y = arr[i][6]; 
      third.x = arr[i][7]; 
      third.y = arr[i][8]; 
      fourth.x = arr[i][9]; 
      fourth.y = arr[i][10]; 
      if (a === first.x && b === first.y) {
        arr[i].splice(0,11);
        var newArray = new Array();
        newArray = arr;
        var nar = newArray.filter(function(x) { return (x !== (undefined || null || ''));})
        localStorage.setItem('nar',JSON.stringify(arr));
      }
    }
  }

This does removes the array, but the resultant array nar begins with an empty array.

I tried with pushing each item of arr into newArray, which also returns the array with an empty array.

for (var i = 0; i < arr.length; i++) {
    if (arr[i][0] === "STAR_SPORTS_2-20170924-200043-210917-00142.jpg") {
      first.x = arr[i][3];
      first.y = arr[i][4]; 
      second.x = arr[i][5]; 
      second.y = arr[i][6]; 
      third.x = arr[i][7]; 
      third.y = arr[i][8]; 
      fourth.x = arr[i][9]; 
      fourth.y = arr[i][10]; 
      if (a === first.x && b === first.y) {
        arr[i].splice(0,11);
        var newArray = new Array();
        arr.forEach(function(item) {
          newArray.push(item);
        })
        localStorage.setItem('nar',JSON.stringify(newArray));
      }
    }
  }
Apricot
  • 2,925
  • 5
  • 42
  • 88
  • Possible duplicate of [My Function Is Broken It Seems Javascript](https://stackoverflow.com/questions/23026290/my-function-is-broken-it-seems-javascript) – user202729 Jul 30 '18 at 06:48
  • 2
    (I can't say if it's all of the errors. But `(x !== (undefined || null || ''))` is definitely suspicious) – user202729 Jul 30 '18 at 06:48
  • Can you make a fiddle? I think you want to remove the data from array matches a and b. and store the balance array to localstorage is that you are saying :) – Nair Athul Jul 30 '18 at 06:56
  • @user202729 I am a beginner trying to learn and build an application using javascript....I couldn't figure out why my question is a duplicate – Apricot Jul 30 '18 at 06:56
  • Can you see what's wrong with `(x !== (undefined || null || ''))`? (see the linked possible duplicate above) Can you fix it yourself? – user202729 Jul 30 '18 at 07:03
  • Ok, it's not the main problem here. – user202729 Jul 30 '18 at 07:04

1 Answers1

2

This is the simplest way i think(if i m not wrong)

arr = [["STAR_SPORTS_2-20170924-200043-210917-00142.jpg", "TShirt", "Redshift", 267, 403, 377, 389, 385, 479, 275, 493], 
       ["STAR_SPORTS_2-20170924-200043-210917-00142.jpg", "TShirt", "Redshift", 317, 319, 428, 325, 426, 372, 315, 366],
       ["STAR_SPORTS_2-20170924-200043-210917-00142.jpg", "TShirt", "Redshift", 425, 490, 539, 425, 526, 472, 215, 478]]
 var newArray=[];   
arr.forEach(function(d) {
if(d[0]=="STAR_SPORTS_2-20170924-200043-210917-00142.jpg" && d[3]!=267 && d[4]!=403){newArray.push(d); }
});
//localStorage.setItem('nar',JSON.stringify(newArray));
console.log(newArray);
NullPointer
  • 7,094
  • 5
  • 27
  • 41