从Vue JS中的数组对象获取数组中的ID

我是Vue js的新手,使用vue-multiselect时,数组对象中的数据如下所示。

    [
        {
            "id": 1,"add_on_type": "Xtra","name": "test","price": 12,"created_at": "2020-06-25 10:12:43","updated_at": "2020-06-25 10:12:43"
        },{
            "id": 3,"name": "Some x","price": 120,"created_at": "2020-06-30 05:47:52","updated_at": "2020-06-30 05:47:52"
        }
    ]

但是在我的函数中,我需要像下面的key:value一样访问

"xtra": {
  // key: value
  0: 1
  1: 3
}

但是我得到了所有的数组对象,而不仅仅是id。我只需要在数组中获取ID,下面是我的代码。我不知道如何使用下面的代码从id中仅获取array

this.$axios
      .get("items/" + this.item)
      .then(res => {
        // below line is how I get the array object,but I need only id in array.
        data.xtra = this.extra;
        console.log(data);
      })
      .catch(err => {
        throw err;
      });

这对某些人来说可能很容易,但是我找不到解决方法。任何帮助,将不胜感激。预先感谢

iCMS 回答:从Vue JS中的数组对象获取数组中的ID

如果我正确理解了您的问题,则this.item持有从数组中检索到的对象。如果是这样,它应该像这样简单:

  .get("items/" + this.item.id)
,

如果您要创建新的数组,可以从axios返回时执行此操作

.then(res => {
  let arr = res.data
  this.xtra = arr.map(x =>
  x.item.id)

 })
本文链接:https://www.f2er.com/2029687.html

大家都在问