我有两个对象,第一个如下:
raw:{item:100,nested:{nested1:20,nested2:100}}
第二个具有相同的结构但提供一些固定值范围:
范围:{item:&#39; 20,60&#39 ;,嵌套:{nested1:&#39; 20,80&#39;,nested2:&#39; 30,110&#39;}} < / p>
我想循环检查&#39; raw&#39;中的每个值。如果它在范围&#39;范围内,则返回如下结果:
{item:false,nested:{nested1:true,nested2:true}}
var range = {
blood: {
hemoglobin: '20, 50'
},
pulse: '20, 60'
}
var data = {
blood: {
hemoglobin: 30
},
pulse: 30
}
_.mixin({
deeply: function(map) {
return function(obj, fn) {
return map(_.mapValues(obj, function(v) {
if (_.isPlainObject(v)) {
return _.deeply(map)(v, fn);
} else if (_.isArray(v)) {
return _.map(v, item => _.deeply(map)(item, fn));
}
return v;
}), fn);
}
},
});
var obj = _.deeply(_.mapKeys)(data, function(val, key) {
// ...
});
console.log(obj)
&#13;
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
&#13;
答案 0 :(得分:0)
您可以使用mergeWith作为配对data
对象和range
对象的方法,并确定哪些值包含在某个范围内。
var result = _.mergeWith(_.cloneDeep(data), range, function(d, r) {
if(!_.isObject(d)) {
return _(r).split(',').map(_.trim).map(Number)
.thru(_.spread(function(x, y) {
return d >= x && d <= y;
})).value();
}
});
var range = {
blood: {
hemoglobin: '20, 50'
},
pulse: '20, 60'
};
var data = {
blood: {
hemoglobin: 30
},
pulse: 30
};
var result = _.mergeWith(_.cloneDeep(data), range, function(d, r) {
if(!_.isObject(d)) {
return _(r).split(',').map(_.trim).map(Number)
.thru(_.spread(function(x, y) {
return d >= x && d <= y;
})).value();
}
});
console.log(result);
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>