如何在创建过程中使用Javascript中的循环向数组添加对象?

如何在创建过程中使用Javascript中的循环向数组添加对象?,javascript,Javascript,我正在尝试为网站编写一些元信息(使用vue meta),我需要在名为meta的数组中添加一些标记作为对象 代码如下所示: metaInfo() { return { htmlAttrs: { lang: "en" }, title: this.Post.Title, meta: [ {

我正在尝试为网站编写一些元信息(使用
vue meta
),我需要在名为
meta
的数组中添加一些
标记作为对象

代码如下所示:

metaInfo() {
            return {
                htmlAttrs: { lang: "en"
                },
                title: this.Post.Title,
                meta: [
                    {
                        name: "description", content: this.Post.Title
                    },
                    {
                        name: "date", content: this.Post.DateCreated
                    },
                    {
                        name: "author", content: this.Post.Author
                    },

               // Now I need multiple objects of: {name: "tag", content: "Tags.TagName"} like this but doesn't work:
               function() {
                    this.Tags.forEach(function (TagName, index) {

                    { property: "tag", content: "TagName" }

                    })
                    }
                ],
        }
        }
如何创建我的数组,以便最终得到以下结果,例如:

 meta: [
                    {
                        name: "description", content: "Javascript question"
                    },
                    {
                        name: "date", content: "20200421"
                    },
                    {
                        name: "author", content: "volumeone"
                    },
                    {   property: "tag", content: "Javascript" }
                    ,
                    {   property: "tag", content: "Programming" }
                    ,
                    {   property: "tag", content: "Newbie" }

                ]

除非我遗漏了什么,否则你可以使用推送传递对象

var meta=[];
push({“属性”:“标记”,“内容”:“测试”});
console.log(meta)
你可以做这种事


因此,我只能在创建
meta
数组后使用循环添加到数组中,而不能在创建过程中使用。变量应位于循环外部,而
meta.push()应位于循环内部loop@volumeone,则不能按当前方式向数组中添加。您必须在循环内部初始化或使用push,但不能像现在这样作为返回。@imvain2谢谢,终于明白了:)JS与我所习惯的完全不同,因此必须首先创建整个
meta
数组,然后作为
metaInfo()的一部分返回
我的代码中的方法?让我向您发送完整的示例,然后您将清楚地了解更新的答案,并让我知道您是否理解
              var meta = [{
name: "description", content: this.Post.Title
},
{
name: "date", content: this.Post.DateCreated
},
{
name: "author", content: this.Post.Author
}]

this.Tags.forEach(function (TagName, index) {
  meta.push({ property: "tag", content: "TagName" })
})
metaInfo() {
            return {
                htmlAttrs: { lang: "en"
                },
                title: this.Post.Title,
// or you can just write "meta" instead of "meta: meta" its an shorthand // code 
                    meta: meta
        }

    }