更新时间:2022-01-02 09:33:16
我明白了,问题在于复杂性是平方的.有一个技巧可以做到,只需使用关联数组"即可.
I see, the problem there is that the complexity is squared. There is one trick to do it, it's simply by using "Associative arrays".
您可以获取数组,对其进行循环,然后将数组的值作为键添加到关联数组中.由于它不允许重复键,您将自动删除重复项.
You can get the array, loop over it, and add the value of the array as a key to the associative array. Since it doesn't allow duplicated keys, you will automatically get rid of the duplicates.
由于您在比较时正在寻找标题和艺术家,您实际上可以尝试使用以下内容:
Since you are looking for title and artist when comparing, you can actually try to use something like:
var arrResult = {};
for (i = 0, n = arr.length; i < n; i++) {
var item = arr[i];
arrResult[ item.title + " - " + item.artist ] = item;
}
然后您只需再次循环 arrResult,并重新创建数组.
Then you just loop the arrResult again, and recreate the array.
var i = 0;
var nonDuplicatedArray = [];
for(var item in arrResult) {
nonDuplicatedArray[i++] = arrResult[item];
}
更新以包含保罗的评论.谢谢!