1

here's the code:

data = [
    {
        'id': 'asdja',
        'username': 'james',
    },
    {
        'id': 'asqweja',
        'username': 'rhou',
    },
    {
        'id': 'asdqweqj',
        'username': 'arianne'
    },
    {
        'id': 'qpoaksl',
        'username': 'ryan'
    }
];

I'm trying to check if username already exists.

For example if I input "james" then it should display "Username already exists"

I try to used find:

if (username === data.find((x: any) => x.username === x.username) {
 console.log('Username already exists');
} else {
 console.log('');
}
5
  • What have done so far to achieve it? Commented Feb 28, 2020 at 1:17
  • @VaritJPatel i try use find then still doesn't work. Commented Feb 28, 2020 at 1:18
  • please add whatever you tried to do so far. It helps the community to understand what went wong. Commented Feb 28, 2020 at 1:19
  • Answered as per your solution and explain to you what was wrong you're doing. Commented Feb 28, 2020 at 1:46
  • @Panda, data.find return null or element, so must be if (data.find((x: any) => x.username === x.username). when you compare object, NOT compare that has the same properties, e.g. obj={a:10}; obj1={a:10}; bool equals=obj===obj2; equals is FALSE Commented Feb 28, 2020 at 8:31

5 Answers 5

5

You can use Javascript Array some for it which returns a boolean when the condition is or isn't met.

const inputName = 'james';

// You can use destructuring assignment ({ }) if you only want to use/
// extract a certain property, in our case we will only use the username
const isUserExists = data.some(({ username }) => username === inputName);

console.log(isUserExists); 
Sign up to request clarification or add additional context in comments.

Comments

3

const data = [
    {
        id: 'asdja',
        username: 'james',
    },
    {
        id: 'asqweja',
        username: 'rhou',
    },
    {
        id: 'asdqweqj',
        username: 'arianne'
    },
    {
        id: 'qpoaksl',
        username: 'ryan'
    }
];

const user = data.find((x) => x.username === 'james')

if (user) {
 console.log('Username already exists');
} else {
 console.log('');
}

The issue is that find function returns the first element of the array

Hence, you will get the object back in the response and now you need to check it with the username

const user = data.find((x) => x.username === 'james')

if (user) {
 console.log('Username already exists');
} else {
 console.log('');
}

hope this helps!

Comments

1

What you are trying to do is lookup the data array (which is an array of objects) for a given username and want to know if it exists or not. You could simply use filter() to see if you get an empty list or not. If it returns an empty list then the username does not exist else it does.

var nameToCheck = 'james'
function checkUsername({username}) {
  return username===nameToCheck
}

var res = data.filter(checkUsername);

console.log(res===[] ? 'Username does not exists!' : 'Username already exists!');

Comments

1

You can also directly check in if condition using filter-length

const data = [{id:'asdja',username:'james',},{id:'asqweja',username:'rhou',},{id:'asdqweqj',username:'arianne'},{id:'qpoaksl',username:'ryan'}];
let username = 'james';

if (data.filter(({ username }) => username == username).length) {
 console.log('User already exists');
} else {
  console.log('New User');
}

Comments

0

Another way (using index)

const username: string = "james";
const idx: number = data.findIndex(obj => obj.username === username);
if (idx > -1) {
// Username already exists.
} else {
// Username does not exists.

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.