skip to Main Content

I have following array

[{

        "games1": [{
            "playername": "1"
        }]
    },
    {

        "games2": [{
                "playername": "1"
            },
            {
                "playername": "2"
            }
        ]
    }
]

I want to delete games2 from the array how to do this
I want this type of output

[{

        "games1": [{
            "playername": "1"
        }]
    }
}]

3

Answers


  1. Use filter to filter what you want to keep.

    Like this:

    const arr = [
                {
                    games1: [
                        {
                            playername: '1',
                        },
                    ],
                },
                {
                    games2: [
                        {
                            playername: '1',
                        },
                        {
                            playername: '2',
                        },
                    ],
                },
            ]
    
    // keep games1
    
    const newArr = arr.filter((r) => r.games1)
    console.log(newArr)
    
            // remove games2, keep others
    const newArr2 = arr.filter((r) => !r.games2)
    console.log(newArr2)
    
    

    output would be

    [
        {
            "games1": [
                {
                    "playername": "1"
                }
            ]
        }
    ]
    
    Login or Signup to reply.
  2. const NewArray = array.filter((item) => item !== "games2");
    
    console.log(NewArray)
    
    Login or Signup to reply.
  3. Try the below code:

     let final = x.filter(x=>{
         let c = Object.getOwnPropertyNames(x)
        return c!="games2"
     })
    

    here the output of the ‘final’ is :

    [{
    
            "games1": [{
                "playername": "1"
            }]
        }
    }]
    

    output

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search