如何解决如何从对象数组中获得最小最小值值?
[
{
"id":1,"type":"type1","selling_price":2199,},{
"id":2,"selling_price":4999,{
"id":3,"selling_price":6999,{
"id":4,"type":"type2","selling_price":1999,{
"id":5,"selling_price":2399,{
"id":6,"selling_price":2999,}
]
我只想提取type =“ type1”的最低售价。 type1的最低价格应为2199 我尝试使用以下方法,但无法正常工作
_.map(arr,price => {
if(price.type=="type1") {
let pp =_.min(price,function(minPrice){ return minPrice.selling_price; });
console.log(pp)
}
});
编辑@VLAZ建议的代码
data() {
return {
minSellingPrice:'',arr:[]
}
},method() {
leastPrice() {
if(this.arr) {
const result = _.chain(arr)
.filter(item => item.type === "type1")
.map('selling_price')
.min()
.value()
this.minSellingPrice=result;
}
else this.minSellingPrice=''
}
}
默认情况下,this.minSellingPrice应该为空,但是只要this.minSellingPrice显示为无穷大,就使用此代码。
解决方法
您使用函数minBy和lodash的过滤器功能
let filter_arr = _.filter(arr,function(o) { return o.type=='type1';})
_.minBy(filter_arr,function(o) {
return o.selling_price;
});
它将给出如下结果
{id: 1,selling_price: 2199,type: "type1"}
希望它会为您提供帮助
,您做的映射错误。回调中有条件,一次只能给您一项。相反,您应该filter()
首先将收藏仅设置为所需的类型,然后再获得min()
的价格。
Underscore具有_.chain()
method,可让您对同一数据集执行多项操作,而无需保存中间结果。为了获得最终结果,您需要调用.value()
来表示将不再链接任何调用:
const arr = [ { "id":1,"type":"type1","selling_price":2199,},{ "id":2,"selling_price":4999,{ "id":3,"selling_price":6999,{ "id":4,"type":"type2","selling_price":1999,{ "id":5,"selling_price":2399,{ "id":6,"selling_price":2999,} ];
const result = _.chain(arr)
.filter(item => item.type === "type1")
.min('selling_price')
.value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.11.0/underscore-min.js"></script>
如果只需要价格,而不是对象本身,则可以使用map()
来提取该属性:
const arr = [ { "id":1,} ];
const result = _.chain(arr)
.filter(item => item.type === "type1")
.map('selling_price')
.min()
.value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.11.0/underscore-min.js"></script>
如果要在数组为空的情况下使用后备值,则当前代码将返回Infinity
,因为这是min
的默认返回值。不幸的是,Underscore没有足够全面的API来轻松解决它。一种简单的方法是只检查该值,然后用另一个值代替。
if (result === Infinity) {
result = null;
}
如果只想使用Undescore,则可以选择通过mixin()
定义自己的方法。许多方法都可以在这里解决问题,但作为通用的辅助方法,我会选择or()
,它可以让您与另一个交换值:
/**
* Generic function that either value or if the value fails a test a fallback
* @param {*} value - value to be tested or substituted
* @param {*} otherValue - fallback in case `value` fails the test
* @param {Function} [test] - what the value would be tested against. By default it fails for null or undefined
*/
_.mixin({
or: function(value,otherValue,test = x => x != null ) {
return test(value) ? value : otherValue ;
}
});
const fullArray = [ { "id":1,} ];
const emptyArray = [];
function getMinimum(arr) {
return _.chain(arr)
.filter(item => item.type === "type1")
.map('selling_price')
.min()
.or("",_.isFinite) //use the or() mixin and pass a different test function that rejects `Infinity`
.value();
}
const fullResult = getMinimum(fullArray);
const emptyResult = getMinimum(emptyArray);
console.log(`fullResult is [${fullResult}]`);
console.log(`emptyResult is [${emptyResult}]`); //empty string
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.11.0/underscore-min.js"></script>
最后,如果您喜欢Underscore,但发现API有点局限,例如在这种情况下,建议您切换到Lodash。 非常非常接近Undescore,因为它在过去的某个时候分裂了。 Lodash具有更一致的界面和更多功能,因此在很多情况下,它是对Underscore的直接升级或至少在同等水平上。您想要在Lodash中表达的功能是这样的:
const fullArray = [ { "id":1,} ];
const emptyArray = [];
function getMinimum(arr) {
return _.chain(arr) //no need for .chain() as Lodash implicitly chains via this
.filter(item => item.type === "type1")
.map('selling_price')
.min() //min defaults to `undefined` if nothing is found
.defaultTo(''); // similar to the `or()` mixin above but only for `undefined`,`null`,or `NaN`
.value()
}
const fullResult = getMinimum(fullArray);
const emptyResult = getMinimum(emptyArray);
console.log(`fullResult is [${fullResult}]`);
console.log(`emptyResult is [${emptyResult}]`); //empty string
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
与Underscore不同,Lodash支持开箱即用的所有功能来完成整个操作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。