簡體   English   中英

在JS中訪問對象屬性

[英]Accessing an object property in JS

我目前正在寫博客,有點卡住了,我需要一些幫助!

用戶創建文章時,可以設置特殊標簽,以通過屬性“位置”將文章推到網站頂部。 因此article.position可以等於“第一”,“第二”,“第三”或“無”(這是默認位置),具體取決於用戶設置的內容。

這是使用axios創建文章的方法:

addArticle() {

  axios.post('http://localhost:4000/articles', {
     title: this.title,
     description: this.description,
     content: this.content,
     position: this.position,
    })

     .then(function (response) {
       console.log(response);
     })
     .catch(function (error) {
       console.log(error);
      });
   },

一切工作正常,但這是問題所在:

當用戶創建具有特殊位置的文章時,如果文章已經設置為相同位置,我希望將其替換為用戶剛剛設置的新文章。

例如:一條舊文章A的position == "first" 用戶正在創建新的商品B,並且他希望商品B位於頂部,他將position == "first" 文章現在應將職位設置為“無”。

重要說明:文章位於Vuex商店中。

這是我在axios請求上方的addArticle方法中嘗試的操作:

if (article.position == "first") {

  let articlesList = this.$store.state.articles

  for (var i = 0; articlesList.length; i++) {
    if(articlesList[i].position == "first")
      articlesList[i].position == "none"
  }
}

我是菜鳥,所以可能有很多錯誤,我在做什么錯?

感謝您的時間!

  if (article.position === 'first') {

    let articlesList = this.$store.state.articles;

    for (i = 0; i < articlesList.length; i++) {
      if(articlesList[i].position === 'first')
        articlesList[i].position = "none"
    }
  }

我只是在不提及最佳做法和內容的情況下進行最小的更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM