本文主要是介绍js常用方法(未完待续...),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
数字转大写
toChinesNum(num) {let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; //changeNum[0] = "零"let unit = ['', '十', '百', '千', '万'];num = parseInt(num);let getWan = temp => {let strArr = temp.toString().split('').reverse();let newNum = '';for (var i = 0; i < strArr.length; i++) {newNum =(i == 0 && strArr[i] == 0 ? '' : i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? '' : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i])) + newNum;}return newNum;};let overWan = Math.floor(num / 10000);let noWan = num % 10000;if (noWan.toString().length < 4) noWan = '0' + noWan;return overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num);
},
查找值在数组对象中的下标
let index = this.feeList.findIndex(val => {return val.charge_item_id == e;
});
数组对象某一列逗号拼接字符串
let idStr = '';
this.selWaitList.forEach(item => {idStr += item.charge_list_id + ',';
});
idStr = idStr.substring(0, idStr.length - 1);
数组过滤对象
this.personList = res.data.person.filter(item=>{return item.type != '负责人'
})
两数组合并为一个数组对象
let c = [10,11,12,13]
let c1 = ['张三','李四','王五','梨花']
let newArr1 = c.map((age, i) => ({age, name: c1[i]}))
数组对象取一部分对象属性到新的数组
let unitList = this.multipleSelection.map(item => {return {name: item.name,age:item.age}});
数组对象重复值过滤(重复只保留一个)
const res = new Map();
this.renewUnitList = unitList.filter((arr) => !res.has(arr.area_name) && res.set(arr.area_name, 1));
根据id数组,在数组对象找到一样的id对象,找到的id名逗号拼接字符串
this.locationList = [{id:1,name:张三},{id:2,name:小明},{id:3,name:小红}]
let idList = [1,2]
let filteredArr = this.locationList.filter(obj => idList.includes(obj.id));
// 将过滤结果转换成字符串
let filteredStr = [];
for (let obj of filteredArr) {filteredStr.push(`${obj.name}`);
}filteredStr = filteredStr.join(',');//张三,小明
这篇关于js常用方法(未完待续...)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!