js 数组常见操作-删除,替换,切割,截取………

js 数组常见操作-删除,替换,切割,截取………Array prototype remove function val varindex this indexOf val if index 1 this splice index 1 v

大家好,欢迎来到IT知识分享网。

indexOf //删除指定数组数据---非常好用 Array.prototype.remove = function(val) { 
    var index = this.indexOf(val); if (index > -1) { 
    this.splice(index, 1); } }; var data =[1,2,3,4,5,6] data.remove(index); //index你要删除的数 
split切割字符串返回数组 var dome ="干饭-跑步-起床-睡觉"; var splitAdd = dome.split("-"); console.log(splitAdd) // ["干饭", "跑步", "起床", "睡觉"] 
splice 删除常规数组 let list = [1,2,3,4,5,6,7] list.splice(2,1) console.log(list) //[1,2,4,5,6,7] 
slice截取字符串,数组 let list = [1,2,3,4,5,6,7] list.slice(2,5) console.log(list) //[3,4,5,6] 
replace 替换 let list = [1,2,3,4,5,6,7] list.replace('2','100') console.log(list) //[1,100,3,4,5,6,7] 
循环对象 const obj= { 
    name: '1', age: 1, profession: '1' }; Object.keys(obj).forEach(key => { 
    console.log(key + ': ' + obj[key]); }); 
Object.assign 合并对象 let obj1 = { 
    a: 1, b: 2 }; let obj2 = { 
    b: 3, c: 4 }; let obj3 = Object.assign(obj1, obj2); console.log(obj3); // { a: 1, b: 3, c: 4 } 解构方式 let obj3 = { 
   ...obj1, ...obj2}; console.log(obj3); // { a: 1, b: 3, c: 4 } 
filter 过滤 数组对象 const people = [ { 
    name: 'Alice', age: 25 }, { 
    name: 'Bob', age: 17 }, { 
    name: 'Charlie', age: 20 }, ]; const filteredPeople = people.filter(person => person.age >= 18); console.log(filteredPeople); // [{ name: 'Alice', age: 25 }, { name: 'Charlie', age: 20 }] 过滤数组 const numbers = [1, 2, 3, 4, 5, 6]; const filteredNumbers = numbers.filter(num => num % 2 !== 0); console.log(filteredNumbers); // [1, 3, 5] 
//查找元素是对象 let items = [ { 
   id: 1, name: 'something'}, { 
   id: 2, name: 'anything'}, { 
   id: 3, name: 'nothing'}, ]; let item = items.find(item => { 
    return item.id == 3; }); console.log(item) //Object { id: 3, name: "nothing" } vue 视图更新数组失败 let a = ['1','2'] this.$set(a,0,1) 强制更新 this.$forceUpdate() 

vue 路由跳转级接收参数常用,容易忘记

1.跳转方式 刷新页面参数 可能会消失 this.$router.push({ 
    name: "message_content_info",//router文件下 路由自定义的name params: { 
    id: row }, //携带参数 }); B页面接收参数 this.$route.params.id; params传参由params接收 2.跳转方式 、、刷新页面参数不会消失 this.$router.push({ 
    path: "message/content/info",//router文件下 路由自定义的name query: { 
    id: row }, //携带参数 }); B页面接收参数 this.$route.query.id; query传参由query接收 
单独校验表单 dataForm this.$refs.dataForm.validateField('name'); 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/125163.html

(0)
上一篇 2025-09-29 20:26
下一篇 2025-09-29 20:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信