Kumail Hussain
Kumail Hussain

Reputation: 879

Update Objects of Objects after Deleting Data

I am stuck in a situation where I need to update my object of objects to update , the real problem is i want to update its index with the deleted item for example:

    {
  "selected": null,
  "lists": {
    "0": [
      {
        "key": "Survey Meta Data"
      }
    ],
    "1": [
      {
        "key": "Assessment"
      }
    ],
    "2": [
      {
        "key": "Income Survey Process"
      }
    ],
    "3": [
      {
        "key": "Appeal Process"
      }
    ]
  }
}

suppose i deleted the lists[0]:

now the current items will be like:

     {
  "selected": null,
  "lists": {
    //"0": [  as deleted so no need just update the others without losing data
     // {
       // "key": "Survey Meta Data"   
     // }
    //],
    "0": [ //now it gets 0
     {
        "key": "Assessment"
      }
    ],
    "1": [//now it gets 1
      {
        "key": "Income Survey Process"
      }
    ],
    "2": [//now it gets 2
      {
        "key": "Appeal Process"
      }
    ]
  }
}

app.js

   function add(data)
{
    for (var i=0;i<4;i++)
                     {

                             $scope.models.lists[i]=[{"key":data[i].id}]



 }   
                     }

function remove(key)
{
   for (var i=0;i<$scope.len;i++)
            {
                    for (var s=0;s<$scope.models.lists[i].length;s++)
                        {


                            if(key==$scope.models.lists[i][s].key) {
                               delete $scope.models.lists[i]// deleted now want to update 
                                break;
                            }
                        }
}
   }

Upvotes: 0

Views: 543

Answers (2)

Jonas Wilms
Jonas Wilms

Reputation: 138267

Quite simple. Dont delete the wanted but the last, and copy one position backward after the element you want to delete:

function remove(key){
    var found=false;
    var list=$scope.models.lists;
    //get the length to properly iterate, requires proper keys e.g. 0,1,2,3
    var length=+Object.keys(list).sort().reverse()[0]+1;
    //iterate over
    for (var i=0;i<length-1;i++){
        if(key===list[i][0].key || found) {
           //if found override current with next
           found=true;
           list[i]=list[i+1];
        }
     }
    //delete last one if weve found sth before, or if the last is the one to be deleted
    if(list[i][0].key===key || found){
        delete list[i];
    }
}

http://jsbin.com/casikuxoha/edit?console

or with multiple:

http://jsbin.com/tewaveyiwa/1/edit?console

Upvotes: 1

user4676340
user4676340

Reputation:

In my previous answer I was completely oblivious of your needs.

Here is the code that allows you to delete any item of the list (copy/paste this in a pristine component and look at the console log) :

list = {
  'selected': null,
  lists: {
    '0': [{ 'key': 'Survey Meta Data' }],
    '1': [{ 'key': 'Assessment' }],
    '2': [{ 'key': 'Income Survey Process' }],
    '3': [{ 'key': 'Appeal Process' }]
  }
};

constructor() { }

ngOnInit() {
  this.removeItem('1');
}

removeItem(index: string) {
  let keys = Object.keys(this.list.lists);
  keys = keys.filter(k => k !== index);
  let myNewVar = { selected: null, lists: {} };
  for (let key of keys) { myNewVar.lists[key] = this.list.lists[key]; }
  console.log(myNewVar);
}

Upvotes: 1

Related Questions