0

I have a array like below.

cont arr= [ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]

Can I know how to verify a 'username' is exist in the array object?

1

1 Answer 1

0

if you want to check if a key exist in all elements of the array you can do this

const arr= [ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]



const existsKey = (data, key) => data.every(d => d.hasOwnProperty(key))

console.log('username', existsKey(arr, 'username'))

console.log('anotherKey', existsKey(arr, 'anotherKey'))

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.