javascript - How to form field associations within an object?
问题描述
const appData = {
articleCount: 0,
articles: [{
id: 1,
title: 'How to make money',
author: users[0].id,
content: 'Gather income streams',
createdat: '30/08/2018'
}],
users: [{
id: 1,
firstname: 'Michael',
lastname: 'Lee',
username: 'AuraDivitiae',
articles: [{id: 1}]
}]
}
I want inside my appData object and the field author to have an association to another field within the same object, is this possible?
解决方案
您可以在对象初始化后执行此操作:
appData.articles[0].author=appData.users[0].id;
const appData = {
articleCount: 0,
users: [{
id: 1,
firstname: 'Michael',
lastname: 'Lee',
username: 'AuraDivitiae',
articles: [{id: 1}]
}],
articles: [{
id: 1,
title: 'How to make money',
author: null,
content: 'Gather income streams',
createdat: '30/08/2018'
}]
}
appData.articles[0].author= appData.users[0].id;
console.log(appData.articles[0].author);
- 用于
Getter
const appData = {
articleCount: 0,
users: [{
id: 1,
firstname: 'Michael',
lastname: 'Lee',
username: 'AuraDivitiae',
articles: [{id: 1}]
}],
articles: [{
id: 1,
title: 'How to make money',
content: 'Gather income streams',
createdat: '30/08/2018',
get author () {
return appData.users[0].id;
}
}]
}
//appData.articles[0].author= appData.users[0].id;
console.log(appData.articles[0].author);
推荐阅读
- github - 如何使用 rmarkdown::render_site() 为课程创建 github.io 页面
- r - R图表上的多条线
- python - 如何更改熊猫分组箱线图中的组标题?
- asp.net-core - microsoft graph api - 以纯文本形式返回的错误响应
- java - 操作的递归顺序
- python - 如何将所有黑色像素更改为白色(OpenCV)?
- java - 无法在android上下载图像并存储为位图
- python - 从网站生成和下载 tsv(使用 python)
- java - 如何修复 intellij 中阻止我运行项目的错误
- html - 属性:text-decoration-skip-ink 不存在:无