我下面有对象。
car = [{
Acc: "X1",
primary: true,
Name: "Park Street",
Date: "2018/01/01"
},{
Acc: "X1",
primary: false,
Name: "Park Street 2",
Date: "2018/03/01"
},{
Acc: "X2",
primary: false,
Name: "Park Street 3",
Date: "2018/01/01"
},{
Acc: "X2",
primary: false,
Name: "Park Street 4",
Date: "2018/05/01"
}];
我只需要从“ Acc”的每个组中获取一个元素。 Acc:“ X1”和Acc:“ X2”中的每一个元素。逻辑上找到应该为primary:true的逻辑,然后从组中选择该元素,否则选择具有最新Date:值的元素。
我确实知道使用下划线js可以实现此要求,但我是新手,无法弄清楚相关方法。
有人可以在这里帮助我吗?
答案 0 :(得分:2)
执行此操作的一种方法是,首先按Acc
进行分组,然后在组中按primary
和Date
降序排序,然后仅选择每个组中的第一个。 / p>
例如
const car = [{
Acc: "X1",
primary: true,
Name: "Park Street",
Date: "2018/01/01"
},{
Acc: "X1",
primary: false,
Name: "Park Street 2",
Date: "2018/03/01"
},{
Acc: "X2",
primary: false,
Name: "Park Street 3",
Date: "2018/01/01"
},{
Acc: "X2",
primary: false,
Name: "Park Street 4",
Date: "2018/05/01"
}];
//lets group
const grouped = car.reduce((a, v) => {
if (!a[v.Acc]) a[v.Acc] = [];
a[v.Acc].push(v);
return a;
}, {});
//now for each group sort by primary, and then Date
const result = Object.values(grouped).map((g) => {
g.sort((a, b) => b.primary - a.primary || b.Date.localeCompare(a.Date));
return g[0]; //now sorted, pick first.
});
console.log(result);
另一种更有效的方法可能只是跟踪对象文字中的最佳内容。
例如。
const car = [{
Acc: "X1",
primary: true,
Name: "Park Street",
Date: "2018/01/01"
},{
Acc: "X1",
primary: false,
Name: "Park Street 2",
Date: "2018/03/01"
},{
Acc: "X2",
primary: false,
Name: "Park Street 3",
Date: "2018/01/01"
},{
Acc: "X2",
primary: false,
Name: "Park Street 4",
Date: "2018/05/01"
}];
const best = {};
car.forEach((c) => {
const b = best[c.Acc] = best[c.Acc] || c;
if (b.primary) return;
if (c.Date.localeCompare(best.Date) < 0) best[c.Acc] = c;
});
console.log(Object.values(best));
答案 1 :(得分:0)
由于您提到了Underscore,所以我了解您可以使用外部库。此方法与@Keith相同,只是使用Lodash。
此外,这应该适用于所有浏览器。
var car = [{
Acc: "X1",
primary: true,
Name: "Park Street",
Date: "2018/01/01"
},{
Acc: "X1",
primary: false,
Name: "Park Street 2",
Date: "2018/03/01"
},{
Acc: "X2",
primary: false,
Name: "Park Street 3",
Date: "2018/01/01"
},{
Acc: "X2",
primary: false,
Name: "Park Street 4",
Date: "2018/05/01"
}];
var sorted = _.orderBy(car, ['primary', 'Date'], ['desc', 'desc']);
var groupedSorted = _.groupBy(sorted, 'Acc');
var result = _.flatMap(groupedSorted, function(o) { return o[0]; });
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
答案 2 :(得分:0)
如果primary:true,则从组中选择该元素,否则选择具有最新Date:值的元素
根据上述说明,每个组(primary:true
或X1
)中只有一个X2
。如果我的理解是正确的,则可以尝试使用以下代码:
// Input object
var car = [{
Acc: "X1",
primary: true,
Name: "Park Street",
Date: "2018/01/01"
}, {
Acc: "X1",
primary: false,
Name: "Park Street 2",
Date: "2018/03/01"
}, {
Acc: "X2",
primary: false,
Name: "Park Street 3",
Date: "2018/01/01"
}, {
Acc: "X2",
primary: false,
Name: "Park Street 4",
Date: "2018/05/01"
}];
// fetch the primary:true objects from the Input array.
var primaryTrueArray = car.filter(obj => {
if (obj.primary === true) {
return obj;
}
});
// filtered out the primary:true objects from the Input array based on the primaryTrueArray.Hence, we will get all primary:false.
var filteredOutPrimaryTrue = car.filter(obj => primaryTrueArray.filter(item => item.Acc != obj.Acc));
// Fetch the latest date from the group who has primary:false from the filteredOutPrimaryTrue.
var latestDateFromGroup = filteredOutPrimaryTrue.sort(function(a,b){
if (a.Date < b.Date)
return 1;
else if (a.Date == b.Date)
return 0;
else
return -1;
});
// Concatenate both the arrays "primaryTrueArray & latestDateFromGroup" into single one.
var finalResult = primaryTrueArray.concat([latestDateFromGroup[0]]);
// Desired output.
console.log(finalResult);