Panda
Panda

Reputation: 415

How to check if the array data already exists in angular

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('');
}

Upvotes: 1

Views: 7810

Answers (5)

Ryan Gek
Ryan Gek

Reputation: 9

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.

Upvotes: 0

Sameer
Sameer

Reputation: 5188

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');
}

Upvotes: 1

sudo97
sudo97

Reputation: 914

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!');

Upvotes: 1

Varit J Patel
Varit J Patel

Reputation: 3520

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!

Upvotes: 3

KShewengger
KShewengger

Reputation: 8269

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); 

Upvotes: 5

Related Questions