检查嵌套数组中是否存在值
Check if value exists in nested array
我正在尝试检查嵌套数组中是否存在某个值。这是我正在处理的数据的样子:
[
{
name: 'bob',
occupation: ['Teacher', 'Store Owner'],
},
{
name: 'grace',
occupation: ['Doctor'],
},
]
我正在尝试查看职业值是否已经存在。这是我当前的代码:
const occupationExists = (value) => users.some((user) => user.occupation === value);
我知道这是行不通的,因为它没有访问数组中的值,但我该怎么做呢?任何帮助将不胜感激。
您还需要检查 occupation
以及 Array#includes
。
const occupationExists = value => users.some(user =>
user.occupation.includes(value)
);
let aoo = [
{
name: 'bob',
occupation: ['Teacher', 'Store Owner'],
},
{
name: 'grace',
occupation: ['Doctor', 'Store Owner'],
},
]
let fullOccupation = []
aoo.map(obj => {
obj.occupation.map(occ => {
const check = fullOccupation.includes(occ)
console.log(occ)
if(!check)
{
fullOccupation.push(occ)
}else{
alert(occ)
}
})
})
这应该可以解决问题,它 returns true/false 取决于职业是否存在。
let users = [
{
name: 'bob',
occupation: ['Teacher', 'Store Owner'],
},
{
name: 'grace',
occupation: ['Doctor'],
},
]
const occupationExists = (value) => {
let res = users.filter((user) =>
user.occupation.indexOf(value) != -1
)
return res.length > 0
}
let value = 'Doctor'
console.log(occupationExists(value))
这是 user.occupation === value
的问题:user.occupation
是一个字符串数组,而 value
是一个字符串,因此您无法比较两者。您可以按照@NinaScholz 的说明使用 Array#includes()
,也可以使用另一个(内部)Array#some()
,在其中您可以将字符串与字符串进行比较:job === value
.
const users = [{name: 'bob',occupation: ['Teacher', 'Store Owner']}, {name: 'grace',occupation: ['Doctor']}];
//const occupationExists = value => users.some(user => user.occupation.includes(value)); //best answer
//Alternative answer
const occupationExists = value => users.some(
user => user.occupation.some(job => job === value)
);
console.log( occupationExists('Teacher') );
console.log( occupationExists('Developer') );
我正在尝试检查嵌套数组中是否存在某个值。这是我正在处理的数据的样子:
[
{
name: 'bob',
occupation: ['Teacher', 'Store Owner'],
},
{
name: 'grace',
occupation: ['Doctor'],
},
]
我正在尝试查看职业值是否已经存在。这是我当前的代码:
const occupationExists = (value) => users.some((user) => user.occupation === value);
我知道这是行不通的,因为它没有访问数组中的值,但我该怎么做呢?任何帮助将不胜感激。
您还需要检查 occupation
以及 Array#includes
。
const occupationExists = value => users.some(user =>
user.occupation.includes(value)
);
let aoo = [
{
name: 'bob',
occupation: ['Teacher', 'Store Owner'],
},
{
name: 'grace',
occupation: ['Doctor', 'Store Owner'],
},
]
let fullOccupation = []
aoo.map(obj => {
obj.occupation.map(occ => {
const check = fullOccupation.includes(occ)
console.log(occ)
if(!check)
{
fullOccupation.push(occ)
}else{
alert(occ)
}
})
})
这应该可以解决问题,它 returns true/false 取决于职业是否存在。
let users = [
{
name: 'bob',
occupation: ['Teacher', 'Store Owner'],
},
{
name: 'grace',
occupation: ['Doctor'],
},
]
const occupationExists = (value) => {
let res = users.filter((user) =>
user.occupation.indexOf(value) != -1
)
return res.length > 0
}
let value = 'Doctor'
console.log(occupationExists(value))
这是 user.occupation === value
的问题:user.occupation
是一个字符串数组,而 value
是一个字符串,因此您无法比较两者。您可以按照@NinaScholz 的说明使用 Array#includes()
,也可以使用另一个(内部)Array#some()
,在其中您可以将字符串与字符串进行比较:job === value
.
const users = [{name: 'bob',occupation: ['Teacher', 'Store Owner']}, {name: 'grace',occupation: ['Doctor']}];
//const occupationExists = value => users.some(user => user.occupation.includes(value)); //best answer
//Alternative answer
const occupationExists = value => users.some(
user => user.occupation.some(job => job === value)
);
console.log( occupationExists('Teacher') );
console.log( occupationExists('Developer') );