【问题标题】:Remove whole object from array in vue从Vue中的数组中删除整个对象
【发布时间】:2018-07-25 00:14:55
【问题描述】:

现在我有一个包含多家公司的数组。

像这样,但只有1个:

[{
    "id": 4,
    "name": "Lorem",
    "description": "Lorem ipsum"
}]

我遍历公司并通过删除功能传递公司。

<a @click="deleteCompany(company)"><i class="fa fa-trash" aria-hidden="true"></i></a>

deleteCompany(company) {
    Vue.delete(company, 'name');
}

到目前为止,它只删除了名称.. 但是如何删除整个对象?

【问题讨论】:

    标签: arrays object vue.js


    【解决方案1】:

    假设您的公司数组在 Vue 的数据中,您可以这样删除它:

    deleteCompany(company) {
        // find the index of the company object in the companies array
        const where = this.companies.findIndex(c => c === company)
        // splice the object from the array
        this.companies.splice(where, 1)
    }
    

    如果您选择将公司的索引传递给函数而不是公司本身,这可能会缩短一点。

    您也可以使用Vue.delete,但在这种情况下没有必要;拼接会做。如果你想使用 Vue.delete,它看起来像这样:Vue.delete(this.companies, where)。

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2015-08-12
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 2015-08-29
    • 2018-05-13
    • 1970-01-01
    • 2016-11-08
    • 2020-02-01
    相关资源
    最近更新 更多