【发布时间】:2022-11-15 06:35:16
【问题描述】:
如果要删除数据,我会使用 findIndex 找到元素并将其作为 null 值丢弃在 isInArray 中。如果没有这样的数据,如何将它添加到从第一个元素开始的空元素中?例如,如果第一个数据中的元素是满的,它应该知道第二个元素是空的并添加它。
<template>
<input type="text" v-model="name">
<input type="text" v-model="surname">
<button @click="addCustomer"></button>
</template>
<script>
import Vue from "vue";
export default {
data(){
return{
name:null,
surname:null,
customers:[{},{},{}],
currentIndex:null
}
},
methods:{
addCustomer(){
let findIndex = this.customers.findIndex((customer) => customer.name === this.name );
let isInArray = findIndex !== -1;
if(isInArray){
Vue.set(this,"currentIndex",findIndex)
Vue.set(this.customers[this.currentIndex], 'name', null)
}else{
// What Should I write here?
}
}
}
}
</script>
【问题讨论】:
-
目前尚不清楚您要达到什么目的。
-
@YardenBuzaglo 很清楚。查看客户数组。如果它已满,则不会向其添加数据,如果下一个索引为空,则会向其添加数据。
标签: javascript vue.js vuejs2