数组方法
① push pop unshift shift
② splice slice concat join includes
③ indexof fill map find findIndex
// 图片数据整理 (将已有数据数组中的每一项的[部分或全部]字段赋值给新字段当成对象返回)
skuInfo.skuImageList = imageList.map((item) => {
return {
imgName: item.imgName,
imgUrl: item.imgUrl,
isDefault: item.isDefault,
spuImgId: item.id
}
})
④ every()和some()
every() 判断数组中每一项都是否满足条件,只有所有项都满足条件,才会返回true。
some() 判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true。
//判断是否全选
isAllCheck() {
//简写 return this.carInfoList.every(item => item.isChecked === 1)
this.carInfoList.every((item) => {
return item.isChecked === 1
})
}
// 2. 判断新添加元素属性值 是否与 属性值数组有重复
let isRet = this.attrInfo.attrValueList.some((item) => {
// 排除新添加元素,row为新添加元素
if (row !== item) {
return row.valueName === item.valueName
}
})
⑤ sort排序
var arr = [20,10,2,1,3];
arr.sort();// [1, 10, 2, 20, 3]
arr.sort(function(a,b){
return a-b; //升序
}); //[1, 2, 3, 10, 20]
arr.sort(function(a,b){
return b-a; //降序
}); //[20,10,3,2,1]
⑥ reduce()方法 (求和,去重)
skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
if (item.attrIdAndValueId) {
const [attrId, valueId] = item.attrIdAndValueId.split(':')
prev.push({ attrId, valueId })
}
return prev
}, [])
⑦ entries( ) 方法:遍历键值对 keys( ) 方法:遍历键名 values( ) 方法:遍历键值
entries( ) 方法:遍历键值对 -----------------------------------------------------
for(let [key, value] of ['a', 'b'].entries()){
console.log(key, value);
}
// 0 "a"
// 1 "b"
// 不使用 for... of 循环
let entries = ['a', 'b'].entries();
console.log(entries.next().value); // [0, "a"]
console.log(entries.next().value); // [1, "b"]
// 数组含空位
console.log([...[,'a'].entries()]); // [[0, undefined], [1, "a"]]
keys( ) 方法:遍历键名 -----------------------------------------------------
for(let key of ['a', 'b'].keys()){
console.log(key);
}
// 0
// 1
// 数组含空位
console.log([...[,'a'].keys()]); // [0, 1]
values( ) 方法:遍历键值-----------------------------------------------------
for(let value of ['a', 'b'].values()){
console.log(value);
}
// "a"
// "b"
// 数组含空位
console.log([...[,'a'].values()]); // [undefined, "a"]
⑧ parseInt & parseFloat
<script>
//parseFloat()函数 可解析一个字符串,并返回一个向下取整的数
let a = "10.9"
let b = 10.9
console.log(parseInt(a)) // 10
console.log(parseInt(a)) // 10
//parseFloat()函数 可解析一个字符串,并返回一个浮点数。
document.write(parseFloat("10") + "<br>"); //10
document.write(parseFloat("10.33") + "<br>"); //10.33
document.write(parseFloat("34 45 66") + "<br>"); //34
document.write(parseFloat(" 60 ") + "<br>"); //60
document.write(parseFloat("40 years") + "<br>"); //40
document.write(parseFloat("He was 40") + "<br>"); // NaN
</script>
⑨
⑩
ES6-11新语法
① …操作符(合并对象,数组)
let person1 = {name: "Amy", age: 15}
let person2 = {name: "Amy", age: 16}
var newArr = {...person1, ...person2};
console.log(newArr) //{name: 'Amy', age: 16}
var arr1 = [1, 2, 3]
var arr2 = ['a', 'b', 'c']
var newArr = [...arr1, ...arr2]; //[1, 2, 3, "a", "b", "c"];
②Object原型新增方法
assign 合并对象
const obj1 = {a:1,b:2,c:3}
const obj2 = {a:2,b:3,d:4}
console.log(Object.assign(obj1,obj2));//不冲突的都保留,冲突的后面的值覆盖前面的值
重置数据,看情况而定,如果data中的数据都需要清理时可以使用
初始化所有数据
Object.assign(this.$data,this.$options.data())
初始化某个对象中的数据
Object.assign(this.$data.xxx,this.$options.data().xxx)
this.$data //这个是现在阶段的vue数据,就是你改变后data的数据
this.$options.data() //这个是vue原始的数据,就是你页面刚加载时的data
// 如果想恢复个别值,比如ref为formInline的表单数据 三种方法
①this.formInline = this.$options.data.call(this).formInline
②Object.assign(this.$data.formInline, this.$options.data.call(this).formInline);
③this.$refs.formInline.resetFields();
③ promise的方法
promise.all()
let requestArr = [this.$API.sku.reqSpuImageList(spu.id), this.$API.sku.reqSpuSaleAttrList(spu.id), this.$API.sku.reqAttrInfoList(category1Id, category2Id, spu.id)]
Promise.all(requestArr).then(
(resArr) => {
this.spuImageList = resArr[0].data
this.spuImageList = resArr[1].data
this.attrInfoList = resArr[2].data
}, (error) => {
console.log(y, 'error')
})