[英]JavaScript can I directly access to a property inside a property in the object?
In this object, I want to print out an array in the form of [userName, skills].在这个object中,我想以[userName, skills]的形式打印出一个数组。 I know that these objects don't have indexes.我知道这些对象没有索引。 Is it possible to detect the skills
property and only print out the value of that?是否可以检测skills
属性并只打印出它的值?
const users = {
Alex: {
email: 'alex@alex.com',
skills: ['HTML', 'CSS', 'JavaScript'],
age: 20,
isLoggedIn: false,
points: 30
},
Asab: {
email: 'asab@asab.com',
skills: ['HTML', 'CSS', 'JavaScript', 'Redux', 'MongoDB', 'Express', 'React', 'Node'],
age: 25,
isLoggedIn: false,
points: 50
},
Brook: {
email: 'daniel@daniel.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux'],
age: 30,
isLoggedIn: true,
points: 50
}
}
I first tried the code below but there was an error that it cannot read properties of undefined (reading 0).我首先尝试了下面的代码,但出现错误,它无法读取未定义的属性(读取 0)。
let userId = Object.keys(users); //(3) ['Alex', 'Asab', 'Brook']
for (let i = 0; i < userId.length; i++) {
let userSkills = users.userId[i].skills;
console.log(userId, userSkills);
}
Is it the only way that I check all the skills one by one like below?这是我像下面这样一一检查所有技能的唯一方法吗?
console.log(users.Alex.skills);
console.log(users.Asab.skills);
console.log(users.Brooks.skills);
As said in comments, you should use bracket notation.如评论中所述,您应该使用括号表示法。
Links:链接:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors
https://javascript.info/object https://javascript.info/object
Also, you can use this to shorten your code:此外,您可以使用它来缩短代码:
userId.forEach(user => console.log(users[user].skills));
About array methods you can read here:关于数组方法,您可以在此处阅读:
https://javascript.info/array-methods https://javascript.info/array-methods
Try this, It will work for you试试这个,它会为你工作
let userId = Object.keys(users); //(3) ['Alex', 'Asab', 'Brook']
for (let i = 0; i < userId.length; i++) {
let userSkills = users[userId[i]].skills;
}
const users = {
Alex: {
email: 'alex@alex.com',
skills: ['HTML', 'CSS', 'JavaScript'],
age: 20,
isLoggedIn: false,
points: 30
},
Asab: {
email: 'asab@asab.com',
skills: ['HTML', 'CSS', 'JavaScript', 'Redux', 'MongoDB', 'Express', 'React', 'Node'],
age: 25,
isLoggedIn: false,
points: 50
},
Brook: {
email: 'daniel@daniel.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux'],
age: 30,
isLoggedIn: true,
points: 50
}
}
for (const element in users) {
console.log(users[element].skills);
}
You can use Object.entries
and map for it您可以使用Object.entries
和 map
const users = { Alex: { email: 'alex@alex.com', skills: ['HTML', 'CSS', 'JavaScript'], age: 20, isLoggedIn: false, points: 30 }, Asab: { email: 'asab@asab.com', skills: ['HTML', 'CSS', 'JavaScript', 'Redux', 'MongoDB', 'Express', 'React', 'Node'], age: 25, isLoggedIn: false, points: 50 }, Brook: { email: 'daniel@daniel.com', skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux'], age: 30, isLoggedIn: true, points: 50 } } const resultArray = Object.entries(users).map(([user, {skills}]) => [user, skills]) const resultObject = Object.entries(users).map(([user, {skills}]) => ({user, skills})) console.log(resultArray) console.log(resultObject)
let userId = Object.keys(users); //(3) ['Alex', 'Asab', 'Brook']
userId.forEach(u=> {
console.log(users[u].skills);
});
You're almost there -你快到了 -
for (let i = 0; i < userId.length; i++) {
let userSkills = users[userId[i]].skills;
console.log(userId[i], userSkills);
}
Use Bracket Notation instead of Dot Notation.使用括号表示法而不是点表示法。 Because all the time dot notation is not acceptable.因为所有时间点符号都是不可接受的。 Like -.number, .dependingValue, .anyCalculation, numericValue.something像 -.number, .dependingValue, .anyCalculation, numericValue.something
Javascript mixes up decimal. Javascript 混淆了十进制。
Read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors阅读更多: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors
for (name in users) {
console.log([name, ...users[name].skills])
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.