目前,我有一个数组如下:
var uniqueCount = Array();
几步之后,我的数组看起来像这样:
uniqueCount = [a,b,c,d,d,e,a,b,c,f,g,h,h,h,e,a];
我该如何计算数组中有多少个a、b、c?我希望得到这样的结果:a = 3
b = 1
c = 2
d = 2
目前,我有一个数组如下:
var uniqueCount = Array();
几步之后,我的数组看起来像这样:
uniqueCount = [a,b,c,d,d,e,a,b,c,f,g,h,h,h,e,a];
我该如何计算数组中有多少个a、b、c?我希望得到这样的结果:a = 3
b = 1
c = 2
d = 2
代码:
function getUniqueDataCount(objArr, propName) {
var data = [];
if (Array.isArray(propName)) {
propName.forEach(prop => {
objArr.forEach(function(d, index) {
if (d[prop]) {
data.push(d[prop]);
}
});
});
} else {
objArr.forEach(function(d, index) {
if (d[propName]) {
data.push(d[propName]);
}
});
}
var uniqueList = [...new Set(data)];
var dataSet = {};
for (var i = 0; i < uniqueList.length; i++) {
dataSet[uniqueList[i]] = data.filter(x => x == uniqueList[i]).length;
}
return dataSet;
}
片段
var data= [
{day:'Friday' , name: 'John' },
{day:'Friday' , name: 'John' },
{day:'Friday' , name: 'Marium' },
{day:'Wednesday', name: 'Stephanie' },
{day:'Monday' , name: 'Chris' },
{day:'Monday' , name: 'Marium' },
];
console.log(getUniqueDataCount(data, ['day','name']));
function getUniqueDataCount(objArr, propName) {
var data = [];
if (Array.isArray(propName)) {
propName.forEach(prop => {
objArr.forEach(function(d, index) {
if (d[prop]) {
data.push(d[prop]);
}
});
});
} else {
objArr.forEach(function(d, index) {
if (d[propName]) {
data.push(d[propName]);
}
});
}
var uniqueList = [...new Set(data)];
var dataSet = {};
for (var i = 0; i < uniqueList.length; i++) {
dataSet[uniqueList[i]] = data.filter(x => x == uniqueList[i]).length;
}
return dataSet;
}
var uniqueCount="a","b","c","d","d","e","a","b","c","f","g","h","h","h","e","a"];
var count=0;
var obj={};
uniqueCount.forEach((i,j)=>{
count=0;
var now=i;
uniqueCount.forEach((i,j)=>{
if(now==uniqueCount[j]){
count++;
obj[i]=count;
}
});
});
console.log(obj);
var testArray = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a'];
这是一个包含重复元素的数组,其中包括了字母a到h。
var newArr = [];
testArray.forEach((item) => {
newArr[item] = testArray.filter((el) => {
return el === item;
}).length;
})
console.log(newArr);
声明一个对象arr
来保存唯一的键集合。使用map循环遍历数组一次来填充arr
。如果之前没有找到该键,则添加该键并分配值为零。在每次迭代中增加键的值。
给定testArray:
var testArray = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a'];
var arr = {};
testArray.map(x=>{ if(typeof(arr[x])=="undefined") arr[x]=0; arr[x]++;});
JSON.stringify(arr)
会输出
{"a":3,"b":2,"c":2,"d":2,"e":2,"f":1,"g":1,"h":3}
Object.keys(arr)
会返回 ["a","b","c","d","e","f","g","h"]
要查找任何项(例如 b)的出现次数,arr['b']
将输出 2
var uniqueCount = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a'];
// here we will collect only unique items from the array
var uniqueChars = [];
// iterate through each item of uniqueCount
for (i of uniqueCount) {
// if this is an item that was not earlier in uniqueCount,
// put it into the uniqueChars array
if (uniqueChars.indexOf(i) == -1) {
uniqueChars.push(i);
}
}
// after iterating through all uniqueCount take each item in uniqueChars
// and compare it with each item in uniqueCount. If this uniqueChars item
// corresponds to an item in uniqueCount, increase letterAccumulator by one.
for (x of uniqueChars) {
let letterAccumulator = 0;
for (i of uniqueCount) {
if (i == x) {letterAccumulator++;}
}
console.log(`${x} = ${letterAccumulator}`);
}
demo.js
并在控制台中使用node demo.js
运行它,您将以矩阵形式获得元素的出现次数。var multipleDuplicateArr = Array(10).fill(0).map(()=>{return Math.floor(Math.random() * Math.floor(9))});
console.log(multipleDuplicateArr);
var resultArr = Array(Array('KEYS','OCCURRENCE'));
for (var i = 0; i < multipleDuplicateArr.length; i++) {
var flag = true;
for (var j = 0; j < resultArr.length; j++) {
if(resultArr[j][0] == multipleDuplicateArr[i]){
resultArr[j][1] = resultArr[j][1] + 1;
flag = false;
}
}
if(flag){
resultArr.push(Array(multipleDuplicateArr[i],1));
}
}
console.log(resultArr);
您将在控制台中获得以下结果:
[ 1, 4, 5, 2, 6, 8, 7, 5, 0, 5 ] . // multipleDuplicateArr
[ [ 'KEYS', 'OCCURENCE' ], // resultArr
[ 1, 1 ],
[ 4, 1 ],
[ 5, 3 ],
[ 2, 1 ],
[ 6, 1 ],
[ 8, 1 ],
[ 7, 1 ],
[ 0, 1 ] ]
计算字符串中提供的字母数量
function countTheElements(){
var str = "ssdefrcgfrdesxfdrgs";
var arr = [];
var count = 0;
for(var i=0;i<str.length;i++){
arr.push(str[i]);
}
arr.sort();
for(var i=0;i<arr.length;i++){
if(arr[i] == arr[i-1]){
count++;
}else{
count = 1;
}
if(arr[i] != arr[i+1]){
console.log(arr[i] +": "+(count));
}
}
}
countTheElements()
let arr=[1,2,3,3,4,5,5,6,7,7]
let obj={}
for(var i=0;i<arr.length;i++){
obj[arr[i]]=obj[arr[i]]!=null ?obj[arr[i]]+1:1 //stores duplicate in an obj
}
console.log(obj)
//returns object {1:1,:1,3:2,.....}
function Check(){
var arr = Array.prototype.slice.call(arguments);
var result = [];
for(i=0; i< arr.length; i++){
var duplicate = 0;
var val = arr[i];
arr.map(function(x){
if(val === x) duplicate++;
})
result.push(duplicate>= 2);
}
return result;
}
测试:
var test = new Check(1,2,1,4,1);
console.log(test);
var count = {};
var arr = ['a', 'b', 'c', 'd', 'd', 'e', 'a', 'b', 'c', 'f', 'g', 'h', 'h', 'h', 'e', 'a'];
var iterator = function (element) {
count[element] = (count[element] || 0) + 1;
}
if (arr.forEach) {
arr.forEach(function (element) {
iterator(element);
});
} else {
for (var i = 0; i < arr.length; i++) {
iterator(arr[i]);
}
}
{}
这种类型的map,而不是函数式编程中的map
。 - Matt Ball