如何从内部有对象的数组中获取键值
How can I get a key value from an array that has object inside
假设我有这样的东西:
const myArray = [
{
Email:example@example.com,
Password:1234,
Username:myname
}
]
如何以更简单的方式访问对象内部的电子邮件值?我知道可以做一些循环或映射然后使用 for of
或类似的东西但有没有更好的方法?
我想做这样的检查:
const otherArray = [
{
Email:example@notexample.com,
Username:myname,
Password:1234
}
]
如果 myArray
中的 Email
值等于 otherArray
的值,则:
return true
一些示例(语法错误已修复在值上)
let myname = "I create sntax errors";
const myArray = [{
Email: "example@example.com",
Password: "1234",
Username: myname
}];
const otherArray = [{
Email: "example@notexample.com",
Username: myname,
Password: "1234"
}];
const otherArrayAlso = [{
Email: "example@example.com",
Username: myname,
Password: "1234"
}];
let isMatch = myArray[0].Email == otherArray[0].Email;
console.log(myArray[0].Email, otherArray[0].Email);
console.log("IsMatch:", isMatch);
console.log(myArray[0].Email == otherArrayAlso[0].Email);
您可以通过 myArray[0].Email
获取电子邮件。但首先将您的数组更改为
const myArray = [
{
'Email': 'example@example.com',
'Password': 1234,
'Username': 'myname'
}
]
您可以像这样简单地检查
const myArray = [
{
Email: 'test@test.com',
Age: 30,
},
{
Email: 'One@test.com',
Age: 24,
},
{
Email: 'Two@test.com',
Age: 20,
},
]
const otherArray = [
{
Email: 'Three@test.com',
Age: 30,
},
{
Email: 'test@test.com',
Age: 24,
},
{
Email: 'Four@test.com',
Age: 20,
},
]
let found = false;
const otherArrayEmails = otherArray.map(item => item.Email);
for (let i = 0; i < myArray.length; i++) {
if (otherArrayEmails.includes(myArray[i].Email)) {
found = true;
break;
}
}
console.log(found)
你可以使用Array.some()方法。
工作演示:
const myArray = [
{
Email: 'example@example.com',
Username: 'myname',
Password: 1234
}, {
Email: 'example1@example.com',
Username: 'myname1',
Password: 5678
}, {
Email: 'example2@example.com',
Username: 'myname2',
Password: 8756
}
];
const otherArray = [
{
Email: 'example@notexample.com',
Password: 1234,
Username: 'myname'
}
];
const res = myArray.some(obj => obj.Email === otherArray[0].Email);
console.log(res); // false as example@example.com is not equal to example@notexample.com.
假设我有这样的东西:
const myArray = [
{
Email:example@example.com,
Password:1234,
Username:myname
}
]
如何以更简单的方式访问对象内部的电子邮件值?我知道可以做一些循环或映射然后使用 for of
或类似的东西但有没有更好的方法?
我想做这样的检查:
const otherArray = [
{
Email:example@notexample.com,
Username:myname,
Password:1234
}
]
如果 myArray
中的 Email
值等于 otherArray
的值,则:
return true
一些示例(语法错误已修复在值上)
let myname = "I create sntax errors";
const myArray = [{
Email: "example@example.com",
Password: "1234",
Username: myname
}];
const otherArray = [{
Email: "example@notexample.com",
Username: myname,
Password: "1234"
}];
const otherArrayAlso = [{
Email: "example@example.com",
Username: myname,
Password: "1234"
}];
let isMatch = myArray[0].Email == otherArray[0].Email;
console.log(myArray[0].Email, otherArray[0].Email);
console.log("IsMatch:", isMatch);
console.log(myArray[0].Email == otherArrayAlso[0].Email);
您可以通过 myArray[0].Email
获取电子邮件。但首先将您的数组更改为
const myArray = [
{
'Email': 'example@example.com',
'Password': 1234,
'Username': 'myname'
}
]
您可以像这样简单地检查
const myArray = [
{
Email: 'test@test.com',
Age: 30,
},
{
Email: 'One@test.com',
Age: 24,
},
{
Email: 'Two@test.com',
Age: 20,
},
]
const otherArray = [
{
Email: 'Three@test.com',
Age: 30,
},
{
Email: 'test@test.com',
Age: 24,
},
{
Email: 'Four@test.com',
Age: 20,
},
]
let found = false;
const otherArrayEmails = otherArray.map(item => item.Email);
for (let i = 0; i < myArray.length; i++) {
if (otherArrayEmails.includes(myArray[i].Email)) {
found = true;
break;
}
}
console.log(found)
你可以使用Array.some()方法。
工作演示:
const myArray = [
{
Email: 'example@example.com',
Username: 'myname',
Password: 1234
}, {
Email: 'example1@example.com',
Username: 'myname1',
Password: 5678
}, {
Email: 'example2@example.com',
Username: 'myname2',
Password: 8756
}
];
const otherArray = [
{
Email: 'example@notexample.com',
Password: 1234,
Username: 'myname'
}
];
const res = myArray.some(obj => obj.Email === otherArray[0].Email);
console.log(res); // false as example@example.com is not equal to example@notexample.com.