【问题标题】:Pushing variables onto an array inside an object? [closed]将变量推送到对象内的数组上? [关闭]
【发布时间】:2019-12-30 05:02:16
【问题描述】:
我正在尝试将数据推送到对象中的学生标签数组的末尾,我想知道如何做到这一点?
var tags = "2019 Student"
{
"first_name": "Thomas",
"last_name": "Lee",
"student_tags":
["2020 Student"]
}
【问题讨论】:
标签:
javascript
arrays
json
google-apps-script
javascript-objects
【解决方案1】:
使用push() 将项目添加到数组中。
let tags = "2019 Student"
let obj = {
"first_name": "Thomas",
"last_name": "Lee",
"student_tags": ["2020 Student"]
};
obj.student_tags.push(tags);
console.log(obj);
【解决方案2】:
在student_tags 上使用push:
obj.student_tags.push(tags);
【解决方案3】:
var tags = "2019 Student"
var student = {
"first_name": "Thomas",
"last_name": "Lee",
"email": "test@email.tel",
"student_tags":
["2020 Student"]
}
您只需转到 student_tags 属性并推送标签即可。
student.student_tags.push(tags);