0

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"
        }]
    }
}]
0

3 Answers 3

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"
            }
        ]
    }
]
Sign up to request clarification or add additional context in comments.

1 Comment

@MohitJain Try this const newArr2 = arr.filter((r) => !r.games2)
0
const NewArray = array.filter((item) => item !== "games2");

console.log(NewArray)

1 Comment

games2 is the name of the key, not the value itself.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.