Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.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/9/three.js/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_Object - Fatal编程技术网

Javascript 数组中对象的比较

Javascript 数组中对象的比较,javascript,object,Javascript,Object,假设我有以下对象数组: myArray = [ { id: 'first', date: '2020-11-30', percentage: 10 }, { id: 'second', date: '2020-10-30', percentage: 20 }, { id: 'first', date: '2020-09-30', percentage: 30 } ] 基本上,我的问题是如何找到所有具有相同值的id,然后比较它们的日期以查看哪个具有更高的值(我计划使用Date.parse转换字符

假设我有以下对象数组:

myArray = [ { id: 'first', date: '2020-11-30', percentage: 10 }, { id: 'second', date: '2020-10-30', percentage: 20 }, { id: 'first', date: '2020-09-30', percentage: 30 } ]
基本上,我的问题是如何找到所有具有相同值的id,然后比较它们的日期以查看哪个具有更高的值(我计划使用Date.parse转换字符串),最后检查哪个具有更大的百分比,然后为条件分配一个变量

我不知道该怎么做,但我想它看起来像下面的代码,还是不一样,提前谢谢你的帮助

 for (i = 0; i < myArray.length; i++) {
     if (myArray.id[i] === myArray.id[i]) {
         if (myArray.date[i] > myArray.date[i]) {
            if (myArray.percentage[i] > myArray.percentage[i]) {
               let stuff = stuff;
           }
        }
     }
 }
for(i=0;imyArray.date[i]){
if(myArray.percentage[i]>myArray.percentage[i]){
让东西=东西;
}
}
}
}

您需要记住前面看到的具有
id
的对象,以便在每次循环迭代中将它们与“现在”看到的对象进行比较。在现代JavaScript中,
Map
是一种很好的方法,或者在ES5中使用
object.create(null)
创建对象

const lastSeen = new Map();
for (const entry of myArray) {
    const {id, date, percentage} = entry;
    const last = lastSeen.get(id);
    if (last) {
        if (date > last.date && percentage > last.percentage) {
            // ...this entry is newer than the previous one with the matching ID
            // Replace the previous one (and possibly do something with `stuff`?)
            lastSeen.set(id, entry);
        }
    } else {
        lastSeen.set(id, entry);
    }
}
实例:

constmyarray=[{id:'first',date:'2020-11-30',percentage:10},{id:'second',date:'2020-10-30',percentage:20},{id:'first',date:'2020-09-30',percentage:30}];
const lastSeen=新映射()
for(myArray的常量条目){
const{id,date,percentage}=条目;
const last=lastSeen.get(id);
如果(最后){
log(`Checking${id}/${date}/${percentage}…`);
如果(日期>最后一个日期和百分比>最后一个百分比){
//…此条目比上一个具有匹配ID的条目新
//替换上一个(并可能使用'stuff'?)
log(`Replacing${id}…`);
lastSeen.set(id,条目);
}否则{
log(`notreplacement${id}`);
}
}否则{
log(`${id}是新的,添加了…`);
lastSeen.set(id,条目);
}

}
您可以使用对象缩小数组,并检查键是否存在,或者所需的属性是否更大

const
数据=[{id:'first',date:'2020-11-30',百分比:10},{id:'second',date:'2020-10-30',百分比:20},{id:'first',date:'2020-09-30',百分比:30}],
结果=对象值(数据减少((r,o)=>{
如果(
!r[o.id]||
r[o.id]。日期

.as控制台包装{max height:100%!important;top:0;}
您期望最终结果是什么?输出这个对象?什么是
let stuff=stuff?嗨,T.J,谢谢你的回答。请您解释一下解决方案,我尝试过实现它,但var last=lastSeen[entry.id]给出的是未定义的。@user3843256-它会在第一次看到id时这样做,但不会在看到以前看到的id时这样做。在这种情况下,
last
将是之前看到的一个,我们将进行比较,以确定是应该用当前的(
entry
)替换它,还是保留上一个(
last
)。我知道当它第一次看到一个ID时就应该这样做,但它仍然显示“undefined”尽管有多个ID具有相同的值。@user3843256-与上面的代码不同,请参阅我刚才添加的代码段。非常感谢,伙计,我只是错误地实现了它。