【发布时间】:2020-07-07 13:42:59
【问题描述】:
在我的 Vue.js 项目中,我有一个对象数组,我想列出这些对象并在浏览器中显示。 我的数组包含四个对象,我只想显示 3 个。我选择 3 个对象的方式取决于用户在项目的其他位置选择并存储在变量中的首选项设置(下面称为 userPreference)。我目前停留在基于 userPreference 值从我的数组中删除一个对象的最佳和最有效的方法上。
我的模板中的 v-for
<ul v-for="item in getOutroItems"><li>item<li></ul>
我的对象:
data() {
return {
outroItems: [{ title: "outro1", text: "XYZ" }, { title: "outro2", text: "ABC" }, { title: "outro3",
text`enter code here`: "QRS" }, { title: "outro4", text: "TUV" }],
userPreference: ""
};
}
我的计算属性(这是我目前拥有的)
getOutroItems() {
this.outroItems.filter((value) => {
if(this.userPreference === "newsletter") {
/// here I want to remove outro2 from my array and return an array with the other 3 values
} else (this.userPreference === "noNewsletter") {
/// here I want to remove outro3 from my array and return an array with the other 3 values
}
})
}
那么,从数组中删除特定元素的最佳方法是什么?
提前致谢,如果有什么不清楚的地方请告诉我。
【问题讨论】:
-
this.userPreference可以有多少个不同的值,或者您刚才提到的只有 2 个值?
标签: javascript arrays vue.js