简体   繁体   English

在JS中访问对象属性

[英]Accessing an object property in JS

I'm currently working on a blog and I'm kinda stuck I need some help! 我目前正在写博客,有点卡住了,我需要一些帮助!

When the user creates an article, he can set a special tag to push an article on top of the website through the property "position". 用户创建文章时,可以设置特殊标签,以通过属性“位置”将文章推到网站顶部。 So article.position can be equal to "first", "second", "third" or "none" (which is the default placement) depending on what the user sets. 因此article.position可以等于“第一”,“第二”,“第三”或“无”(这是默认位置),具体取决于用户设置的内容。

Here is the method for creating an article with axios : 这是使用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);
      });
   },

Everything is working perfectly, but here is the problem : 一切工作正常,但这是问题所在:

When a user creates an article with a special position, if an article is already set to the same position, I want it to be replaced by the new article that the user just set. 当用户创建具有特殊位置的文章时,如果文章已经设置为相同位置,我希望将其替换为用户刚刚设置的新文章。

For example: An old Article A has position == "first" . 例如:一条旧文章A的position == "first" The user is creating a new Article B and he wants Article B to go at the top, he sets position == "first" . 用户正在创建新的商品B,并且他希望商品B位于顶部,他将position == "first" Article A position should be now set as "none". 文章现在应将职位设置为“无”。

Important note : articles are located on a Vuex store. 重要说明:文章位于Vuex商店中。

Here is what I tried, in the addArticle method above the axios request : 这是我在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"
  }
}

I'm a rookie so, it might have a lot of mistakes, what am I doing wrong? 我是菜鸟,所以可能有很多错误,我在做什么错?

Thank you for your time! 感谢您的时间!

  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"
    }
  }

I am just making smallest changes possible without mentioning best practices and stuff. 我只是在不提及最佳做法和内容的情况下进行最小的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM