我已经为这个问题绞尽脑汁一周了,非常感谢任何帮助或建议。我有一个收集用户名字、姓氏和歌曲的表单。它将这些值存储在一个对象中,并且具有相同歌曲属性的所有对象都存储在一个数组中。示例:
[
[
{
firstName: "John",
lastName: "Doe",
song: "Rock",
},
{
firstName: "Emily",
lastName: "Jones",
song: "Rock",
},
{
firstName: "David",
lastName: "Williams",
song: "Rock",
},
],
[
{
firstName: "Alice",
lastName: "Johnson",
song: "Jazz",
},
{
firstName: "John",
lastName: "Doe",
song: "Jazz",
},
{
firstName: "Jane",
lastName: "Smith",
song: "Jazz",
},
],
[
{
firstName: "Jennifer",
lastName: "Miller",
song: "Pop",
},
],
];
用户可以添加多个人并将他们与一首特定的歌曲关联起来。(有时每首歌只有一个人,有时可能有 30 个。因此每个数组的对象数量会有所不同)。表单提交后,最终结果是一个包含多个嵌套数组的数组,每个数组包含多个对象。
最终结果应该是一个包含所有嵌套数组的排序数组,但具有相同名字和姓氏的嵌套数组不能彼此相邻。在上面的示例中,“John Doe”位于第一个数组和第二个数组中。我不能让同名的人在最终数组中彼此相邻。
我厌倦了创建一个比较两个数组的compareArrays(array1, array2)函数,如果它返回true,则将该数组推到父数组的末尾。该解决方案不起作用,因为那样我最终会得到一堆被推到末尾的条目,但仍然与被推到末尾但未进行比较的其他数组相邻。示例:
function compareArrays(array1, array2) {
if (!array1 || !array2) {
return false;
}
const length1 = array1.length;
const length2 = array2.length;
//iterate through each element of array1
for (let i = 0; i < length1; i++) {
//compare array[i] with each element of array2
for (let j = 0; j < length2; j++) {
if (
array1[i].firstName === array2[j].firstName &&
array1[i].lastName === array2[j].lastName
) {
console.log("Match found!");
return true; //A match was found
}
}
}
return false; //No match was found in array1
}
然后我在另一个函数中使用该函数,如果compareArrays()返回true,则会将该数组推到父数组的末尾。
function pushMatchingToEnd(parentArray) {
let length = parentArray.length;
let i = 0;
while (i < length) {
const currentArray = parentArray[i];
let j = i + 1;
while (j < length) {
const nextArray = parentArray[j];
// Compare the current array to the next array
if (compareArrays(currentArray, nextArray)) {
// If they match, push nextArray to the end of parentArray
parentArray.push(nextArray);
// Remove nextArray from its current position
parentArray.splice(j, 1);
// Decrement length to account for the added element
length--;
// Decrement j to stay at the same index in the next iteration
j--;
}
j++;
}
// Move to the next array in parentArray
i++;
}
}
pushMatchingToEnd(arrayOfArrays)
不幸的是,这个解决方案不起作用,因为我最终得到了一堆被推到最后但从未进行比较的数组。所以我通常会得到 5 个左右的数组,这些数组与具有匹配名称的其他数组相邻。
我不会提供所有代码,因为我的 JavaScript 有点“睡着了”,我不想犯错误,所以我会给你解释:
首先你必须违背你的直觉,你必须创建一个列表,将每个用户的条目放在一起,即:
{
firstName: "John",
lastName: "Doe",
song: "Rock",
},
{
firstName: "John",
lastName: "Doe",
song: "Jazz",
},
{
firstName: "John",
lastName: "Doe",
song: "Folk",
},
{
firstName: "Emily",
lastName: "Jones",
song: "Rock",
},
{
firstName: "David",
lastName: "Williams",
song: "Rock",
},
{
firstName: "David",
lastName: "Williams",
song: "Jazz",
},
{
firstName: "Jane",
lastName: "Smith",
song: "Jazz",
},
然后按出现次数对它们进行排序:
{
firstName: "John",
lastName: "Doe",
song: "Rock",
},
{
firstName: "John",
lastName: "Doe",
song: "Jazz",
},
{
firstName: "John",
lastName: "Doe",
song: "Folk",
},
{
firstName: "David",
lastName: "Williams",
song: "Rock",
},
{
firstName: "David",
lastName: "Williams",
song: "Jazz",
},
{
firstName: "Emily",
lastName: "Jones",
song: "Rock",
},
{
firstName: "Jane",
lastName: "Smith",
song: "Jazz",
},
那么您只需将它们输入到输出矩阵中,并使用以下 for:
int old = 0;
for( int i = 0; i < listNew.length; i +=2 ) {
listNew [ i ] = listaOld[ old ];
old++;
}
for( int i = 1; i < listNew.length; i +=2 ) {
listNew [ i ] = listaOld[ viejas ]
old++;
}
如果没有用户的条目数量超过总条目的一半,则应该可以工作。