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]
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');