如何根据javascript中的另一个数组对数组进行排序?
我按出现频率计算段落中的单词,现在我也需要对它们进行排序,例如[this:2,is:3,it:1]到[is:3,this:2,it:1]。我把键和值分成两个不同的数组,然后对一个值数组进行排序,现在我想对一个键数组进行排序如何根据javascript中的另一个数组对数组进行排序?,javascript,arrays,sorting,Javascript,Arrays,Sorting,我按出现频率计算段落中的单词,现在我也需要对它们进行排序,例如[this:2,is:3,it:1]到[is:3,this:2,it:1]。我把键和值分成两个不同的数组,然后对一个值数组进行排序,现在我想对一个键数组进行排序 console.log('app running'); function getFrequencyOfWord(word : string) { let counting: any = {}; let wordSplit: any = word.split('
console.log('app running');
function getFrequencyOfWord(word : string) {
let counting: any = {};
let wordSplit: any = word.split(' ');
wordSplit.forEach(function (word: any) {
if (counting[word]) {
counting[word]++;
}
else {
counting[word] = 1;
}
})
var arr1 = Object.keys(counting);
var arr2 = arr1.map((suboor)=> {
return counting[suboor];
});
for (var i : number = 0; i < arr2.length; i++) {
for (var j = 0; j < (arr2.length -i -1); j++) {
if (arr2[j] > arr2[j+1]) {
const lesser = arr2[j+1];
arr2[j+1] = arr2[j];
arr2[j] = lesser;
}
}
}
console.log(arr2);
console.log(arr1);
}```
console.log('apprunning');
函数getFrequencyOfWord(字:字符串){
让计数:any={};
让wordSplit:any=word.split(“”);
forEach(函数(单词:any){
如果(计算[字]){
计数[字]+;
}
否则{
计数[字]=1;
}
})
var arr1=Object.keys(计数);
var arr2=arr1.map((子对象)=>{
返回计数[或];
});
对于(变量i:number=0;iarr2[j+1]){
常数lesser=arr2[j+1];
arr2[j+1]=arr2[j];
arr2[j]=较小值;
}
}
}
控制台日志(arr2);
控制台日志(arr1);
}```
这不可能根据值数组对键数组进行排序,但您可以通过检查(arr[key]==arr[value])将右键映射到右值,如果键和值相等,则可以将该键值对推送到新数组中 您可以尝试以下方法:
let word = "moo moo moo hello one two three one";
let wordSplit = word.split(' ');
var counting = [];
wordSplit.forEach(function (word) {
if (counting[word]) {
counting[word]++;
}
else {
counting[word] = 1;
}
})
console.log("Counting ...");console.log(counting);
function swap(json){
var ret = {};
for(var key in json){
let element = ret[json[key]] ;
//console.log("element");console.log(element);
if(element == undefined){
ret[json[key]] = element= [];
}
element.push(key);
//console.log("element");console.log(element);
}
return ret;
}
let result = swap(counting);
console.log("RESULT ...");console.log(result);
var finalResult = [];
for(var key in result){
finalResult = finalResult.concat(result[key]);
}
console.log("Final RESULT ...");console.log(finalResult);
输出
小提琴:
更新
问题是你实际上有一个对象的映射而不是数组。对象数组类似于[{is:3},{this:2},{it:1}]。转换并不难。但是,我认为最好有这样的对象{word:X,count:X}
。见下文:
let word = "this this is is it is";
let wordSplit = word.split(' ');
var counting = [];
wordSplit.forEach(function (word) {
if (counting[word]) {
counting[word]++;
}
else {
counting[word] = 1;
}
})
console.log("Counting ...");console.log(counting);
function swap(json){
var ret = {};
for(var key in json){
let element = ret[json[key]] ;
//console.log("element");console.log(element);
if(element == undefined){
ret[json[key]] = element= [];
}
element.push({count:json[key], word:key});
//console.log("element");console.log(element);
}
return ret;
}
let result = swap(counting);
console.log("RESULT ...");console.log(result);
//Reverse it and make it into objects...
let reversedResult = Object.assign([], result ).reverse();
console.log("RESULT-REVERSED ...");console.log(reversedResult);
//Final Conatenated Array
var concatenatedArray = [];
for(var key in reversedResult){
concatenatedArray = concatenatedArray.concat(reversedResult[key]);
}
console.log("CONCATENATED-ARRAY ...");console.log(concatenatedArray);
结果:
0: {count: 3, word: "is"}
1: {count: 2, word: "this"}
2: {count: 1, word: "it"}
小提琴:嗨!欢迎来到stackoverflow!您能否将源数据写入文本,并将所需输出写入文本@Suboor Khan如果某些单词出现的次数相同会发生什么情况。[a:1,b:2,c:2,d:3]到[d:3,b:2,c:2,a:1]但是我确实想从第一个数组对另一个数组排序,但您的代码是Superb@Suboor它回答了你的问题吗?问题是,实际上您有一个贴图或对象,而不是数组。对象数组类似于
[{is:3},{this:2},{it:1}]
。转换并不难。
0: {count: 3, word: "is"}
1: {count: 2, word: "this"}
2: {count: 1, word: "it"}