亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在創建過程中使用 Javascript 中的循環將對象添加到數組中?

如何在創建過程中使用 Javascript 中的循環將對象添加到數組中?

冉冉說 2022-07-08 16:57:37
我正在嘗試為網站編寫一些元信息(使用vue-meta),我需要tags在名為 的數組中添加一些作為對象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" }                ]
查看完整描述

2 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

你可以做這樣的事情。


              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

        }


    }


查看完整回答
反對 回復 2022-07-08
?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

除非我遺漏了什么,否則您可以使用 push 并傳遞對象。


var meta = [];

meta.push({"property" : "tag","content" : "test"});

console.log(meta);


查看完整回答
反對 回復 2022-07-08
  • 2 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號