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
5 Answers
Reset to default 5You 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
版权声明:本文标题:javascript - How to check if the array data already exists in angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741530033a2383700.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论