我需要你的帮助...如果问题已经被问到我很抱歉我似乎无法找到适合我的问题的答案:我正在尝试提取(不是删除)我的数组中重复的列表。
最终,主要目标是只保留一个具有更高利润的重复对象(在数组中)......
这是我的数组的一个简单示例:
var arr = [
{
mkBase: "test",
mkComp: "test1",
invest: { profit: 10 },
availability: true,
option: 1
},
{
mkBase: "test",
mkComp: "test1",
invest: { profit: 15 },
availability: false,
option: 2
},
{
mkBase: "test1",
mkComp: "test",
invest: { profit: 8 },
availability: true,
option: 3
},
{
mkBase: "test2",
mkComp: "test",
invest: { profit: 6 },
availability: true,
option: 4
},
{
mkBase: "test",
mkComp: "test2",
invest: { profit: 6 },
availability: true,
option: 5
},
{
mkBase: "test",
mkComp: "test3",
invest: { profit: 7 },
availability: true,
option: 6
},
{
mkBase: "test",
mkComp: "test3",
invest: { profit: 10 },
availability: true,
option: 7
},
{
mkBase: "test3",
mkComp: "test4",
invest: { profit: 10 },
availability: true,
option: 8
}
];
我设法使用以下方法提取几乎所有重复项的列表:
for (var i = 0; i < arr.length; i++) {
if (_.uniqBy(arr, "mkBase").indexOf(arr[i]) == -1) {
console.log("[SAME BASE]: " + JSON.stringify(arr[i], null, 2));
} else if (_.uniqBy(arr, "mkComp").indexOf(arr[i]) == -1) {
console.log("[SAME COMP]: " + JSON.stringify(arr[i], null, 2));
}
}
这是结果:
[SAME BASE]: {
"mkBase": "test",
"mkComp": "test1",
"invest": {
"profit": 15
},
"availability": false,
"option": 2
}
[SAME COMP]: {
"mkBase": "test2",
"mkComp": "test",
"invest": {
"profit": 6
},
"availability": true,
"option": 4
}
[SAME BASE]: {
"mkBase": "test",
"mkComp": "test2",
"invest": {
"profit": 6
},
"availability": true,
"option": 5
}
[SAME BASE]: {
"mkBase": "test",
"mkComp": "test3",
"invest": {
"profit": 7
},
"availability": true,
"option": 6
}
[SAME BASE]: {
"mkBase": "test",
"mkComp": "test3",
"invest": {
"profit": 10
},
"availability": true,
"option": 7
}
Lodash方法(_.uniqBy)保留主数组中的一个重复项,并且以便最终获得最佳(_.maxBy(arr, 'profit')
)重复项,I需要 其他重复项。
我不确定我是否很清楚,但如果您需要任何澄清,请告诉我!
先谢谢大家!
********** 编辑 ************* 正如stasovlas所建议的那样,你会在下面找到预期的结果,以及为什么数组中的其他对象被删除了:
var result = [
{
mkBase: "test",
mkComp: "test1",
invest: { profit: 15 },
availability: false,
option: 2
},
{
mkBase: "test1",
mkComp: "test",
invest: { profit: 8 },
availability: true,
option: 3
},
{
mkBase: "test3",
mkComp: "test4",
invest: { profit: 10 },
availability: true,
option: 8
}
];
var removed = [
//Reason: Same Base **and** Comp mk as option 2 && Profit is too low versus option 2
{
mkBase: "test",
mkComp: "test1",
invest: { profit: 10 },
availability: true,
option: 1
},
//Reason: Same Comp mk as option 3 && Profit is too low versus option 3
{
mkBase: "test2",
mkComp: "test",
invest: { profit: 6 },
availability: true,
option: 4
//Reason: Same Base mk as option 2 && Profit is too low versus option 2
},
{
mkBase: "test",
mkComp: "test2",
invest: { profit: 6 },
availability: true,
option: 5
},
//Reason: Same Base mk as option 2 && Profit is too low versus option 2
{
mkBase: "test",
mkComp: "test3",
invest: { profit: 7 },
availability: true,
option: 6
},
//Reason: Same Base mk as option 2 && Profit is too low versus option 2
{
mkBase: "test",
mkComp: "test3",
invest: { profit: 10 },
availability: true,
option: 7
}
];
答案 0 :(得分:1)
var sameBase = {}, sameComp = {};
arr.forEach(item => {
let existingBase = sameBase[item.mkBase];
if ( ( existingBase === undefined ) || ( existingBase.invest.profit < item.invest.profit ) ) {
sameBase[item.mkBase] = item;
}
existingComp = sameComp[item.mkComp];
if ( ( existingComp === undefined ) || ( existingComp.invest.profit < item.invest.profit ) ) {
sameComp[item.mkComp] = item;
}
});
var sameBaseArr = toArr(sameBase);
var sameCompArr = toArr(sameComp);
console.log("SAME BASE: " + JSON.stringify(sameBaseArr, true));
console.log("SAME COMP: " + JSON.stringify(sameCompArr, true));
function toArr(map) {
let arr = [];
for (var key in map) {
arr.push(map[key]);
}
return arr;
}
答案 1 :(得分:1)
我在我的问题理解中不确定,但在这里我的解决方案:
const res = _.reduce(arr, (result, item) => {
const same = _.find(result, r => _.some([
r.mkBase === item.mkBase,
r.mkComp === item.mkComp
])); // find same already added item
if (same === undefined) {
return _.concat(result, item); // just push item
}
if (same.invest.profit >= item.invest.profit) {
return result; // do nothing if profit is less then already added
}
return _.chain(result) // remove item with smaller profit and push item with higher profit
.reject({ mkBase: same.mkBase, mkComp: same.mkComp })
.concat(item)
.value();
}, []);
答案 2 :(得分:0)
试试这个(未经过测试,但逻辑可以提供帮助):
var mkBases = [], mkComps = [];
for (var i = 0; i < arr.length; i++) {
var actualBase = arr[i].mkBase;
var actualComp = arr[i].mkComp;
if (typeof mkBases[actualBase] !== 'undefined') {
mkBases[actualBase] = {getAllIndexes(arr, actualBase)}
}
if (typeof mkComps[actualComp] !== 'undefined') {
mkComps[actualComp] = {getAllIndexes(arr, actualComp)}
}
}
function getAllIndexes(arr, val) {
var indexes = [], i = -1;
while ((i = arr.indexOf(val, i+1)) != -1){
indexes.push(i);
}
return indexes;
}
现在你可以遍历mkBases和mkComp来获取你的副本。