javascript基础
JavaScript数组操作:
1、如何根据id找到删除这项的索引
2、直接调用数组的splice方法
this.list.some((item,i) => {
if (item.id = id)
this.list.splice(i, 1)
return true;
})
var index = this.list.findIndex(item => { if (item.id == id) return true }) console.log(index) this.list.splice(index, 1)
// 之前v-for中的数据都是从list中的数据直接渲染过来的 // 现在,自定义一个方法 Search 方法,同事,把 所有的关键字,通过传参的形式传递给Search方法 // 把符合条件的数据组成一个新数组,返回 Search(keywords) { var newList = [] // this.list.forEach(item => { // if (item.name.indexOf(keywords) != -1) { // newList.push(item) // } // }) this.list.filter(item=>{
//ES6中提供的字符串是否包含另一字符串includes,写法:string.prototype.includes('要包含的字符串'),如果包含,返回true,否则false if (item.name.includes(keywords)) { newList.push(item) } }) return newList }
本文来自博客园,作者:CelonY,转载请注明原文链接:https://www.cnblogs.com/CelonY/p/12561953.html