gpt4 book ai didi

javascript - 根据元素出现的频率对数组进行排序

转载 作者:行者123 更新时间:2023-12-01 07:37:09 25 4
gpt4 key购买 nike

我有一个包含重复数据的数组。我想根据频率对其进行排序(首先是最高的重复元素),然后删除重复项。

我尝试了下面的代码,它抛出了不同的顺序。

var arr=[
{name: "Manage AAA Devices", sys_id: "7b491aad371adb003ef7a9c2b3990e22"},
{name: "", sys_id: ""},
{name: "Manage AAA - ISE Admin Functions", sys_id: "dc5f99a2dbb2b74019d81ffa68961933"},
{name: "Manage AAA Devices", sys_id: "7b491aad371adb003ef7a9c2b3990e22"},
{name: "Manage AAA - ISE Admin Functions", sys_id: "dc5f99a2dbb2b74019d81ffa68961933"},
{name: "SNOW Change Request", sys_id: "325b08913783d6c4f4f4c97a43990e90"},
{name: "", sys_id: ""},
{name: "Manage AAA - ISE Admin Functions", sys_id: "dc5f99a2dbb2b74019d81ffa68961933"},
{name: "Manage AAA - ISE Admin Functions", sys_id: "dc5f99a2dbb2b74019d81ffa68961933"},
{name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}
];

console.log(arr.reduce((x, y) => {
if (x[y.name]) {
x[y.name]++;
return x;
} else {
var z = {};
z[y.name] = 1;
return Object.assign(x, z);
}
}, {}))

最佳答案

不是最佳解决方案,但您可以将其用作开始:

var arr = [{
"name": "Manage AAA Devices",
"sys_id": "7b491aad371adb003ef7a9c2b3990e22"
},
{
"name": "",
"sys_id": ""
},
{
"name": "Manage AAA - ISE Admin Functions",
"sys_id": "dc5f99a2dbb2b74019d81ffa68961933"
},
{
"name": "Manage AAA Devices",
"sys_id": "7b491aad371adb003ef7a9c2b3990e22"
},
{
"name": "Manage AAA - ISE Admin Functions",
"sys_id": "dc5f99a2dbb2b74019d81ffa68961933"
},
{
"name": "SNOW Change Request",
"sys_id": "325b08913783d6c4f4f4c97a43990e90"
},
{
"name": "",
"sys_id": ""
},
{
"name": "Manage AAA - ISE Admin Functions",
"sys_id": "dc5f99a2dbb2b74019d81ffa68961933"
},
{
"name": "Manage AAA - ISE Admin Functions",
"sys_id": "dc5f99a2dbb2b74019d81ffa68961933"
},
{
"name": "Incident Request",
"sys_id": "3ee543f61b363740f713ed7b2f4bcbc0"
}
];

const noOfOccurencies = arr.reduce((x, y) => {
if (x[y.name]) {
x[y.name]++;
return x;
} else {
var z = {};
z[y.name] = 1;
return Object.assign(x, z);
}
}, {})

console.log(noOfOccurencies);

// Order an array of objects based on another array order
// https://gist.github.com/ecarter/1423674
function mapOrder(array, order, key) {
array.sort(function(a, b) {
var A = a[key],
B = b[key];

if (order.indexOf(A) > order.indexOf(B)) {
return 1;
} else {
return -1;
}
});
return array;
};

// sort noOfOccurencies
const itemOrder = Object.keys(noOfOccurencies).sort(function(a, b) {
return noOfOccurencies[a] - noOfOccurencies[b]
})
// reverse noOfOccurencies (*highest duplicates element first*)
itemOrder.reverse();

console.log(itemOrder);

// order the original array based on itemOrder
let ordered_array = mapOrder(arr, itemOrder, 'name');

// remove duplicates from the new array ordered
const noDuplicatesArr = ordered_array.filter((v, i, a) => a.findIndex(t => (t.name === v.name)) === i)

console.log(noDuplicatesArr);

关于javascript - 根据元素出现的频率对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61724787/

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