admin管理员组

文章数量:1291348

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

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('');
}
Share Improve this question edited Feb 28, 2020 at 4:32 sudo97 9142 gold badges11 silver badges24 bronze badges asked Feb 28, 2020 at 1:12 PandaPanda 41514 silver badges33 bronze badges 5
  • What have done so far to achieve it? – Varit J Patel Commented Feb 28, 2020 at 1:17
  • @VaritJPatel i try use find then still doesn't work. – Panda Commented Feb 28, 2020 at 1:18
  • please add whatever you tried to do so far. It helps the munity to understand what went wong. – Varit J Patel Commented Feb 28, 2020 at 1:19
  • Answered as per your solution and explain to you what was wrong you're doing. – Varit J Patel 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 pare object, NOT pare that has the same properties, e.g. obj={a:10}; obj1={a:10}; bool equals=obj===obj2; equals is FALSE – Eliseo Commented Feb 28, 2020 at 8:31
Add a ment  | 

5 Answers 5

Reset to default 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); 

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!

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

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

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.

本文标签: javascriptHow to check if the array data already exists in angularStack Overflow