gpt4 book ai didi

javascript - 添加另一个验证以使数据配对工作

转载 作者:行者123 更新时间:2023-12-05 04:19:55 25 4
gpt4 key购买 nike

我正在创建一个匹配功能,其中将配对 2 名体重相同的玩家。

它目前正在基于相同的权重工作。现在,我添加了另一个条件,即玩家有自己的等级。我的第一个条件是基于相同的重量。这是工作。我的第二个条件是关于“CLASS”,如果玩家具有相同的等级,则不应匹配/配对。

const source = [
{
entryID: 1,
entryName: "player1",
weight: 1900,
class: ['a', 'b'],

},
{
entryID: 2,
entryName: "player2",
weight: 1900,
class: ['a', 'b'],

},
{
entryID: 3,
entryName: "player3",
weight: 1900,
class: ['c', 'd'],

},
{
entryID: 4,
entryName: "player4",
weight: 1900,
class: ['c', 'd'],
},

];

console.log(combine(source))

function combine(data = [], different = 0, maxGroupSize = 2) {
const groups = [], related = [], sortedData = [...data].sort((a, b) => a.weight - b.weight),
alreadyInRela = (setX, eName) => {
let list = [...setX, eName]
return related.some(rela => list.every(l => rela.has(l)))
};

sortedData.forEach((el, indx) => {
let place = groups.findIndex( // find a place in a group forEach element, use indx as track
g => g.names.size < maxGroupSize // is the group incomplete ?
&& !g.names.has(el.entryName) // is entryName not in the group list (names Set) ?
&& (el.weight - g.weight) <= different
&& !alreadyInRela(g.names, el.entryName) // is (entryName + group list) does not already used ?
)

if (place < 0) { // not found -> create new group
let names = new Set().add(el.entryName) // create new group
groups.push({ names, indxs: [indx], weight: el.weight }) // group constitutive info
related.push(names) // keep track of group list
} else { // find a place in a group
groups[place].names.add(el.entryName) // related list is also updated
groups[place].indxs.push(indx) // add indx to retreive element in sortedData
}
});

return groups.reduce((r, g, i) => { // build result
if (g.indxs.length > 1) {
let key = `${i}_` + g.indxs.map(x => sortedData[x].weight).join('_')
r[key] = []
g.indxs.forEach(x => r[key].push(sortedData[x]))
}
return r
}, {})
}

我当前的输出:

{
0_1900_1900: [
{
class: ["a", "b"],
entryID: 1,
entryName: "player1",
weight: 1900
},
{
class: ["a", "b"],
entryID: 2,
entryName: "player2",
weight: 1900
}
],
1_1900_1900: [
{
class: ["c", "d"],
entryID: 3,
entryName: "player3",
weight: 1900
},
{
class: ["c", "d"],
entryID: 4,
entryName: "player4",
weight: 1900
}
]
}

目标输出(正如我们在这里看到的,具有相同CLASS的玩家没有加入/合并。这是我需要瞄准的):

{
0_1900_1900: [
{
class: ["a", "b"],
entryID: 1,
entryName: "player1",
weight: 1900
},
{
class: ["c", "d"],
entryID: 3,
entryName: "player3",
weight: 1900
}
],
1_1900_1900: [
{
class: ["a", "b"],
entryID: 2,
entryName: "player2",
weight: 1900
},
{
class: ["c", "d"],
entryID: 4,
entryName: "player4",
weight: 1900
}
]
}

最佳答案

以下代码存储组中的所有类别,并在插入之前比较新玩家的类别:

const source = [
{
entryID: 1,
entryName: "player1",
weight: 1900,
class: ['a', 'b'],

},
{
entryID: 2,
entryName: "player2",
weight: 1900,
class: ['a', 'b'],

},
{
entryID: 3,
entryName: "player3",
weight: 1900,
class: ['c', 'd'],

},
{
entryID: 4,
entryName: "player4",
weight: 1900,
class: ['c', 'd'],
},

];

console.log(combine(source))

function combine(data = [], different = 0, maxGroupSize = 2) {
const groups = [], related = [], sortedData = [...data].sort((a, b) => a.weight - b.weight),
alreadyInRela = (setX, eName) => {
let list = [...setX, eName]
return related.some(rela => list.every(l => rela.has(l)))
};

sortedData.forEach((el, indx) => {
let place = groups.findIndex( // find a place in a group forEach element, use indx as track
g => g.names.size < maxGroupSize // is the group incomplete ?
&& !g.names.has(el.entryName) // is entryName not in the group list (names Set) ?
&& (el.weight - g.weight) <= different
&& !alreadyInRela(g.names, el.entryName) // is (entryName + group list) does not already used ?
&& el.class.every(c => !g.usedClasses.has(c)) // check class
)

if (place < 0) { // not found -> create new group
let names = new Set().add(el.entryName) // create new group
groups.push({ names, indxs: [indx], weight: el.weight, usedClasses: new Set(el.class) }) // group constitutive info
related.push(names) // keep track of group list
} else { // find a place in a group
groups[place].names.add(el.entryName) // related list is also updated
el.class.forEach(c => groups[place].usedClasses.add(c)) // add classes
groups[place].indxs.push(indx) // add indx to retreive element in sortedData
}
});

return groups.reduce((r, g, i) => { // build result
if (g.indxs.length > 1) {
let key = `${i}_` + g.indxs.map(x => sortedData[x].weight).join('_')
r[key] = []
g.indxs.forEach(x => r[key].push(sortedData[x]))
}
return r
}, {})
}

关于javascript - 添加另一个验证以使数据配对工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74637232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com