let util = {
/**
* 原始数组不被修改,将返回一个新数组,新数组只有指定切换的属性和原始数组不一致
* @param arr - 需要置换的数组,此方法将直接在原始数组上修改数据
* @param keys - 需要处理的对象属性
*
* @return - 返回一个新数组
*/
convert : function(arr, keys) {
if (keys instanceof Array) {
keys = new this.Set(keys);
}
let me = this;
let newArray = [];
arr.forEach(function(v) {
let newObject = {};
newArray.push(newObject);
for (let o in v) {
let vv = v[o];
if (keys.contains(o)) {
vv = me.dealDigital(vv);
}
newObject[o] = vv;
}
});
return newArray;
},
dealDigital:function(orig) {
if (orig.length === 1) {
var value = this.zeroMap[orig];
if (value) return value;
return "0.0" + orig;
} else if (orig.length === 2) {
return "0." + orig;
} else {
return orig.substring(0, orig.length - 2) + "." + orig.substring(orig.length - 2);
}
},
Set:function(arr) {
if (this instanceof util.Set) {
this.map = {};
let me = this;
arr.forEach(function(o) {
me.map[o] = true;
});
this.contains = function(key) {
return this.map[key];
}
} else {
return new util.Set(arr);
}
},
zeroMap : {"/":"0.00", "0":"0.00"}
};
//数据
let aaa = [
{
type:'硬币',
useSum:'12000',
remainSum:'0',
basicSum:'0',
useDate:'2019-05-14'},
{
type:'纸币',
useSum:'120000',
remainSum:'10000',
basicSum:'/',
useDate:'2019-05-14'
}
];
//测试调用
let bbb = util.convert(aaa, ["useSum", "remainSum", "basicSum"]);
//打印,得到想要的目标
console.log(bbb);
//aaa无任何变化
console.log(aaa);
let util = {
/**
* 原始数组将被修改,并且返回原始数组
* @param arr - 需要置换的数组,此方法将直接在原始数组上修改数据
* @param keys - 需要处理的对象属性
*
* @return 返回原始数组arr
*/
convert : function(arr, keys) {
if (keys instanceof Array) {
keys = new this.Set(keys);
}
let me = this;
arr.forEach(function(v) {
for (let o in v) {
if (keys.contains(o)) {
v[o] = me.dealDigital(v[o]);
}
}
});
return arr;
},
dealDigital:function(orig) {
if (orig.length === 1) {
var value = this.zeroMap[orig];
if (value) return value;
return "0.0" + orig;
} else if (orig.length === 2) {
return "0." + orig;
} else {
return orig.substring(0, orig.length - 2) + "." + orig.substring(orig.length - 2);
}
},
Set:function(arr) {
if (this instanceof util.Set) {
this.map = {};
let me = this;
arr.forEach(function(o) {
me.map[o] = true;
});
this.contains = function(key) {
return this.map[key];
}
} else {
return new util.Set(arr);
}
},
zeroMap : {"/":"0.00", "0":"0.00"}
};
//数据
let aaa = [
{
type:'硬币',
useSum:'12000',
remainSum:'0',
basicSum:'0',
useDate:'2019-05-14'},
{
type:'纸币',
useSum:'120000',
remainSum:'10000',
basicSum:'/',
useDate:'2019-05-14'
}
];
//测试调用
let bbb = util.convert(aaa, ["useSum", "remainSum", "basicSum"]);
//打印,得到想要的目标
console.log(bbb);
//aaa已经被改变
console.log(aaa);
希望对你有帮助