Search code examples
arraysangular7

how to delete particular json object from array of array


I have array within the array and i want to delete particular JSON object by its value.

rows=[
[isTrue:'false',isAvailable:'false',name:'Abc',data:'ABC',Value:'ABC']
[isTrue:'false',isAvailable:'false',name:'Abc1',data:'ABC1',Value:'ABC1']
[isTrue:'false',isAvailable:'true',name:'Abc2',data:'ABC2',Value:'ABC2']
[isTrue:'false',isAvailable:'true',name:'Abc3',data:'ABC3',Value:'ABC3']
]

I want

rows=[
[name:'Abc',data:'ABC',Value:'ABC']
[name:'Abc1',data:'ABC1',Value:'ABC1']
[name:'Abc2',data:'ABC2',Value:'ABC2']
[name:'Abc3',data:'ABC3',Value:'ABC3']
]

I want to remove all the data which has boolean values present.


Solution

  • The example you have provided has array of objects (not array of array) and should the have following syntax -

    rows=[
        {isTrue:'false',isAvailable:'false',name:'Abc',data:'ABC',Value:'ABC'},
        {isTrue:'false',isAvailable:'false',name:'Abc1',data:'ABC1',Value:'ABC1'},
        {isTrue:'false',isAvailable:'true',name:'Abc2',data:'ABC2',Value:'ABC2'},
        {isTrue:'false',isAvailable:'true',name:'Abc3',data:'ABC3',Value:'ABC3'}
    ]
    

    So, you want to delete the key-value pairs from a JSON object which you can do using -

    for (var row of rows) {    
      delete row['isTrue'];  // this will delete the isTrue key from the object
      delete row['isAvailable'];  // this will delete the isAvailable key from the object
    }
    

    You can check the following link for other ways of deleting a key: Remove a key from a javascript object