qq_45304429
希希莱娜
2020-05-08 09:30
采纳率: 64.3%
浏览 750

求教大神js如何修改数组对象中字符串的值

我设置这样一个数组

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 = [
 {
    type:'硬币',
    useSum:'120.00',
    remainSum:'0.00',
    basicSum:'0.00',
    useDate:'2019-05-14'
    },
 {
     type:'纸币',
     useSum:'1200.00',
     remainSum:'100.00',
     basicSum:'/',
     useDate:'2019-05-14'
     }
 ]

我修改数组数据,本来想直接把useSum,remainSum,basicSum直接转为数字
但是它可能返回‘/’,我应该这么设置,用正则吗

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • w172087242
    little_how 2020-05-08 11:00
    已采纳
    • 方案一:不改变原始数组的值达到替换效果
    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);
    

    希望对你有帮助

    点赞 评论
  • qq_41906761
    人到中年就秃头 2020-05-08 09:53

    this.bbb[0].useSum=this.bbb[0].useSum+'元'

    点赞 评论
  • lele463986946
    lele463986946 2020-05-08 10:29

    遍历当前数组
    bbb[0].basicSum=='/'?0:bbb[0].basicSum/100;

    点赞 评论

相关推荐