Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从JavaScript中的对象数组及其索引中获取最小值?_Javascript - Fatal编程技术网

如何从JavaScript中的对象数组及其索引中获取最小值?

如何从JavaScript中的对象数组及其索引中获取最小值?,javascript,Javascript,我有一个JavaScript对象数组,如下所示: var items = [ { make: 'Lenovo', config1: { price: 388, isCheapest: false, }, config2: { price: 198, isCheapest: false, } conf

我有一个JavaScript对象数组,如下所示:

var items = [
    {
        make: 'Lenovo',
        config1: {
            price: 388,
            isCheapest: false,
        },
        config2: {
            price: 198,
            isCheapest: false,
        }
        config3: {
            price: 176,
            isCheapest: false
        }
    },
    {
        make: 'Dell',
        config1: {
            price: 398,
            isCheapest: false,
        },
        config2: {
            price: 336,
            isCheapest: false,
        }
        config3: {
            price: null,
            isCheapest: false
        }
    },
    {
        make: 'HP',
        config1: {
            price: 264,
            isCheapest: false,
        },
        config2: {
            price: 198,
            isCheapest: false,
        }
        config3: {
            price: 136,
            isCheapest: false
        }
    }
];
如果是最低值,我需要将上述每个配置的
isCheapest
键设置为true。例如,对于config3,HP是最便宜的,因此我必须将HP的
ischeaest
设置为true;而对于config2,联想和HP都是最便宜的,因此我必须将它们的
ischeaest
键都设置为true

还请注意,某些项目的价格值也可以为null。在应用程序中,这意味着该项在该特定配置中不可用。因此,我们不能考虑价格设置为空的项目是最便宜的。< /P>
我尝试为每种配置类型创建3个不同的循环,但它变得非常复杂,有时甚至不准确。我怎样才能做到这一点呢?

好吧,如果你不能改变结构,你就必须编写一些复杂的代码来实现这个目标

transpose=m=>m[0].map((uu,c)=>m.map(r=>r[c]))
最小=(xs,fn)=>xs.reduce((最小,x)=>fn(最小,x)>0?x:min)
转换=项目=>{
var configs=items.map(it=>Object.keys(it)
.sort()
.filter(key=>key.match(/^config/))
.map(key=>it[key]))
转置(配置)
.forEach(xs=>
最小值(xs,(m,x)=>m.price-x.price)
.isCheapest=真);
};
可变项目=[
{
制作:'联想',
配置1:{
价格:388,
isCheapest:错,
},
配置2:{
价格:198,
isCheapest:错,
},
配置3:{
价格:176,
isCheapest:错误
}
},
{
制作:'戴尔',
配置1:{
价格:398,
isCheapest:错,
},
配置2:{
价格:336,
isCheapest:错,
},
配置3:{
价格:764,
isCheapest:错误
}
},
{
make:‘HP’,
配置1:{
价格:264,
isCheapest:错,
},
配置2:{
价格:198,
isCheapest:错,
},
配置3:{
价格:136,
isCheapest:错误
}
}
];
转换(项目)

document.write(“”+JSON.stringify(items,0,3))好吧,如果你不能改变结构,你必须写一些复杂的代码来实现这个目标

transpose=m=>m[0].map((uu,c)=>m.map(r=>r[c]))
最小=(xs,fn)=>xs.reduce((最小,x)=>fn(最小,x)>0?x:min)
转换=项目=>{
var configs=items.map(it=>Object.keys(it)
.sort()
.filter(key=>key.match(/^config/))
.map(key=>it[key]))
转置(配置)
.forEach(xs=>
最小值(xs,(m,x)=>m.price-x.price)
.isCheapest=真);
};
可变项目=[
{
制作:'联想',
配置1:{
价格:388,
isCheapest:错,
},
配置2:{
价格:198,
isCheapest:错,
},
配置3:{
价格:176,
isCheapest:错误
}
},
{
制作:'戴尔',
配置1:{
价格:398,
isCheapest:错,
},
配置2:{
价格:336,
isCheapest:错,
},
配置3:{
价格:764,
isCheapest:错误
}
},
{
make:‘HP’,
配置1:{
价格:264,
isCheapest:错,
},
配置2:{
价格:198,
isCheapest:错,
},
配置3:{
价格:136,
isCheapest:错误
}
}
];
转换(项目)

document.write(“”+JSON.stringify(items,0,3))一个提案,对项目和键进行一些迭代,一个最便宜价格的对象,还有一些迭代

编辑:在结束时对对象
obj
的迭代越小,性能越好

var items=[{make:'Lenovo',config1:{price:388,isCheapest:false,},config2:{price:198,isCheapest:false,},config3:{price:176,isCheapest:false},{make:'Dell',config1:{price:398,isCheapest:false,},config2:{price:336,isCheapest:false,},config3:{price:null,isCheapest:false},{make:'HP',config1:{price:264,isCheapest:false,},配置2:{price:198,isCheapest:false,},配置3:{price:136,isCheapest:false}}];
空洞函数(数据){
var obj={};
项目。forEach(功能(a){
Object.keys(a).forEach(函数(k){
if(在[k]&&a[k]中的[k]=='object'&&&price'类型。price!==null){
if(!(对象中的k)| a[k]。价格document.write(“”+JSON.stringify(items,0,4)+“”);
一个对项目和键进行迭代的提案,一个最便宜价格的对象,还有一些迭代

编辑:在结束时对对象
obj
的迭代越小,性能越好

var items=[{make:'Lenovo',config1:{price:388,isCheapest:false,},config2:{price:198,isCheapest:false,},config3:{price:176,isCheapest:false},{make:'Dell',config1:{price:398,isCheapest:false,},config2:{price:336,isCheapest:false,},config3:{price:null,isCheapest:false},{make:'HP',config1:{price:264,isCheapest:false,},配置2:{price:198,isCheapest:false,},配置3:{price:136
var keys = Object.keys(items[0]).filter(function(key) { 
  return key != 'make'
});

keys.forEach(function(config) {
  var lowestPrice = Number.MAX_SAFE_INTEGER;

  // find the cheapest price of the current config
  items.forEach(function(item) {
    if(item[config].price && item[config].price < lowestPrice) {
      lowestPrice = item[config].price;
    }
  });

  // mark the cheapest configs
  items.forEach(function(item) {
    if(item[config].price && item[config].price <= lowestPrice) {
      item[config].isCheapest = true;
    }
  });
});
var configs = {},
    configProp;

function configSorter(a, b) {
    return a.config.price > b.config.price;
}

items.forEach(function (item, index) {
    var prop,
        rx = /^config\d+$/;

    for (prop in item) {
        if (item.hasOwnProperty(prop)) {
            if (rx.test(prop)) {
                configs[prop] = configs[prop] || [];
                configs[prop].push({
                    item: index,
                    config: item[prop]
                });
            }
        }
    }
});

for (configProp in configs) {
    if (configs.hasOwnProperty(configProp)) {
        configs[configProp].sort(configSorter);

        items[configs[configProp][0].item][configProp].isCheapest = true;
    }
}

console.log(JSON.stringify(items));
[{
    "make": "Lenovo",
    "config1": {
        "price": 388,
        "isCheapest": false
    },
    "config2": {
        "price": 198,
        "isCheapest": true
    },
    "config3": {
        "price": 176,
        "isCheapest": false
    }
}, {
    "make": "Dell",
    "config1": {
        "price": 398,
        "isCheapest": false
    },
    "config2": {
        "price": 336,
        "isCheapest": false
    },
    "config3": {
        "price": 764,
        "isCheapest": false
    }
}, {
    "make": "HP",
    "config1": {
        "price": 264,
        "isCheapest": true
    },
    "config2": {
        "price": 198,
        "isCheapest": false
    },
    "config3": {
        "price": 136,
        "isCheapest": true
    }
}]