//题目:请用于过滤筛选出a数组中rowId,shippingId,countryId,provinceText,startTime一样的数据
//解:
var params = {};
a.forEach((item,index)=>{
if (!params[rowId + '-' + shippingId + '-' + countryId + '-' + provinceText + '-' + startTime]) {
params[rowId + '-' + shippingId + '-' + countryId + '-' + provinceText + '-' + startTime] = {
id:item.id,
.............
}
}
})
params = Object.values(params);
//reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。
//reduce() 对于空数组是不会执行回调函数的
//array.reduce(function(total:初始值, 或者计算结束后的返回值, currentValue:当前元素, currentIndex, arr), initialValue:传递给函数的初始值)
//题目:将[1,2,3,[4,5,[6,7]]]改变成一维数组
//解:
var arrayNeedHandle = [1,2,3,[4,5,[6,7]]]
let flatten = array =>array.reduce((pre,cur)=>{Array.isArray(cur)?pre.push(...flatten(cur)):pre.push(cur);return pre},[])
flatten(arrayNeedHandle)
//深拷贝(针对简单数据类型,复杂数据类型(以及其中含有子类型))
//1.
JSON.parse(JSON.stringify(x))
//通过JSON的官网可知,JSON只支持object,array,string,number,true,false,null这几种数据或者值,其他的比如函数,undefined,Date,RegExp等数据类型都不支持。对于它不支持的数据都会直接忽略该属性。
//2.
function deepClone(target,cache = new Map()){
if(cache.get(target)){
return cache.get(target)
}
if(target instanceof Object){
let dist ;
if(target instanceof Array){
// 拷贝数组
dist = [];
}else if(target instanceof Function){
// 拷贝函数
dist = function () {
return target.call(this, ...arguments);
};
}else if(target instanceof RegExp){
// 拷贝正则表达式
dist = new RegExp(target.source,target.flags);
}else if(target instanceof Date){
dist = new Date(target);
}else{
// 拷贝普通对象
dist = {};
}
// 将属性和拷贝后的值作为一个map
cache.set(target, dist);
for(let key in target){
// 过滤掉原型身上的属性
if (target.hasOwnProperty(key)) {
dist[key] = deepClone(target[key], cache);
}
}
return dist;
}else{
return target;
}
}
//数据去重
let array = [100, 23, 23, 23, 23, 67, 45];
let outputArray = Array.from(new Set(array))
//数组到类数组对象
let arr = ["value1", "value2", "value3"];
let arrObject = {...arr};
//对象到数组
let number = {
one: 1,
two: 2,
};
let key = Object.keys(numbers); // key = [ 'one', 'two' ]
let value = Object.values(numbers); // value = [ 1, 2 ]
let entry = Object.entries(numbers); // entry = [['one' : 1], ['two' : 2]]
//对象遍历
const age = {
Rahul: 20,
max: 16
};
// 方案1:先得 key 在遍历 key
const keys = Object.keys(age);
keys.forEach(key => age[key]++);
console.log(age); // { Rahul: 21, max: 16 }
// 方案2 - `for...in` 循环
for(let key in age){
age[key]++;
}
console.log(age); // { Rahul: 22, max: 18 }