在js中构造json问题

问题描述

在js中构造json的时候如何根据一个值来决定包不包含这个节点?

问题出现的环境背景及自己尝试过哪些方法

当然可以用很多if来搞,比如

            let req = {};            if(this.timeSelect.start != ''){
                req.startTs = this.timeSelect.start;
            }            if(this.timeSelect.end != ''){
                req.endTs = this.timeSelect.end;
            }            if(this.userId != ''){
                req.userId = this.userId;
            }            if(this.auditType != '1'){
                req.checkType = this.auditType;
            }            if(this.opter != ''){
                req.operator = this.opter;
            }            if(this.auditStatus != '0'){
                req.result = this.auditStatus ;
            }

你期待的结果是什么?实际看到的错误信息又是什么?

大神们有什么好的简约的办法么?


Cats萌萌
浏览 784回答 2
2回答

红颜莎娜

/** &nbsp;*&nbsp; &nbsp;*&nbsp;@param&nbsp;{Object}&nbsp;sources&nbsp;传入一个源数据Obj &nbsp;*&nbsp;@param&nbsp;{Array}&nbsp;rule&nbsp;传入一个规则数组&nbsp; &nbsp;*&nbsp;@param&nbsp;{Array}&nbsp;key&nbsp;传入需要判断的key(sources的属性) &nbsp;*&nbsp;key要和rule对应 &nbsp;*/function&nbsp;assigment(sources,&nbsp;rule,&nbsp;key){&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;target&nbsp;=&nbsp;{}&nbsp;&nbsp;&nbsp;&nbsp;for(let&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;key.length;&nbsp;i&nbsp;++){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(rule[i]){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;target[key[i]]&nbsp;=&nbsp;sources[key[i]] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;target }var&nbsp;sources&nbsp;=&nbsp;{a:&nbsp;1,&nbsp;b:&nbsp;'',&nbsp;c:&nbsp;'0'}var&nbsp;rule&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;sources.a&nbsp;!=&nbsp;'', &nbsp;&nbsp;&nbsp;&nbsp;sources.b&nbsp;!=&nbsp;'', &nbsp;&nbsp;&nbsp;&nbsp;sources.c&nbsp;!=&nbsp;'0', ]var&nbsp;key&nbsp;=&nbsp;['a',&nbsp;'b',&nbsp;'c'];var&nbsp;target&nbsp;=&nbsp;assigment(sources,&nbsp;rule,&nbsp;key) console.log(target)

当年话下

你这key值都不对应,估计没对接好吧,只能自己映射了,不过为了优雅和减少代码量,可以简单封装一下:&nbsp;&nbsp;&nbsp;&nbsp;/** &nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;k:&nbsp;req要挂载的key &nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;p:&nbsp;源&nbsp;key &nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;c:&nbsp;上下文取值 &nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;v:&nbsp;不期望的值 &nbsp;&nbsp;&nbsp;&nbsp;**/ &nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;setVal({&nbsp;k,&nbsp;p,&nbsp;c,&nbsp;v&nbsp;=&nbsp;''&nbsp;})&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;_v&nbsp;=&nbsp;c[p]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(_v&nbsp;!=&nbsp;v)&nbsp;req[k]&nbsp;=&nbsp;_v &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;arr&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;k:&nbsp;startTs,&nbsp;p:&nbsp;start,&nbsp;c:&nbsp;this.timeSelect&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;k:&nbsp;endTs,&nbsp;p:&nbsp;end,&nbsp;c:&nbsp;this.timeSelect&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;k:&nbsp;userId,&nbsp;p:&nbsp;userId,&nbsp;c:&nbsp;this&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;k:&nbsp;checkType,&nbsp;p:&nbsp;auditType,&nbsp;c:&nbsp;this,&nbsp;v:&nbsp;'1'&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;arr.forEach(item&nbsp;=>&nbsp;setVal(item))
打开App,查看更多内容
随时随地看视频慕课网APP