Javascript 有没有办法修改数组中允许哪些值的条件?
我有一个值数组。我想在第一个数组的基础上创建第二个具有更严格标准的数组。例如,我特别想要的是:Javascript 有没有办法修改数组中允许哪些值的条件?,javascript,arrays,Javascript,Arrays,我有一个值数组。我想在第一个数组的基础上创建第二个具有更严格标准的数组。例如,我特别想要的是: arrayOne[1,1,1,1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5] 在我的新数组中,只有显示了5次的值才是数组的一部分,并且只显示一次。示例:arrayTwo[1,4] 我对JavaScript相当陌生,有机会为我的一门商业课程编写一个决策系统,而不是做期末考试。如果您能提供任何帮助,我们将不胜感激。谢谢。我用我采取的步骤对代码进行了注释: const arrayO
arrayOne[1,1,1,1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5]
在我的新数组中,只有显示了5次的值才是数组的一部分,并且只显示一次。示例:arrayTwo[1,4]
我对JavaScript相当陌生,有机会为我的一门商业课程编写一个决策系统,而不是做期末考试。如果您能提供任何帮助,我们将不胜感激。谢谢。我用我采取的步骤对代码进行了注释:
const arrayOne=[1,1,1,1,2,2,3,3,3,4,4,4,4,4,5,5];
函数方法(arr,最小值){
//创建一个对象,包含每个数字出现的数量
常量出现次数=arr.reduce((o,n)=>{
//如果对象中已存在编号,请添加1
如果(o[n])o[n]=o[n]+1;
//否则将其发生率设置为1
否则o[n]=1;
//为下一次迭代返回对象
返回o;
}, {});
//重复数据消除阵列创建一个集合(每个元素只能出现一次)并将其分散回阵列
常量重复数据消除=[…新集(arr)];
//筛选数组以仅包含出现次数最少的元素
const filtered=重复数据消除.filter(n=>出现次数[n]>=最小值);
返回过滤;
}
console.log(方法(arrayOne,5))代码>您可以为此使用
let arrayOne = [1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5];
let counterMap = new Map();
arrayOne.forEach(value => {
let valueStr = value.toString();
counterMap.set(valueStr, counterMap.has(valueStr) ? counterMap.get(valueStr) + 1 : 1);
});
let arrayTwo = [];
counterMap.forEach((value, key, map) => {
if(value >= 5) {
arrayTwo.push(key);
}
});
console.log(arrayTwo);
这不是最优雅的答案,但我假设您只是想找到至少出现5次的所有值
const arrayOne=[1,1,1,1,2,2,3,3,3,4,4,4,4,5,5]
常量arrayTwo=Object.entries(arrayOne.reduce((obj,num)=>{
如果(!obj[num]){
对象[num]=1
}否则{
对象[num]=对象[num]+1
}
返回obj
},{})。过滤器(([key,value])=>{
返回值>=5
}).map((项目)=>{
返回parseInt(项[0])
})
log(arrayTwo)
您可以使用一个哈希表,对找到的每个元素进行计数,然后使用计数进行筛选,并在单个循环中仅获取第五个元素作为结果集
var数组=[1,1,1,1,2,2,3,3,4,4,4,4,5,5],
count=Object.create(空),
结果=array.filter(v=>(计数[v]=(计数[v]| | 0)+1)==5);
控制台日志(结果)代码>
定义一个函数,该函数将采用数组
和数值
const filterByOcur = (arr, numOfOccurrences) => {
// create an object to act as a counter this will let us
// iterate over the array only once
const counter = {};
const res = new Set();
for (let num of arr) {
// if it's the first time we see the num set counter.num to 1;
if (!counter[num]) counter[num] = 1;
// if counter.num is greater or equal to numOfOccurrences
// and we don't have the num in the set add it to the set
else if (++counter[num] >= numOfOccurrences && !res.has(num)) res.add(num);
}
// spread the Set into an array
return [...res];
};
console.log(
filterByOcur(a, 5)
);
有很多方法可以做到这一点,我将尝试一步一步地解释:
数组声明
方法来计算数组中的元素数,我们使用的是reducer函数,该函数作为第一个参数接受对象,其中key是数组中的值,并且有一个增量数作为值。记住用空对象启动减速器
使用集合构造函数使数组唯一
在counted array的帮助下,在uniq阵列上启动过滤器功能,看看我们是如何访问它的:
将所有筛选的数组合并为一个数组
为什么[1,4]
?这是一个包含5项的范围?是的,在我的实际代码中,数字是名称(维多利亚、皇后等),我有更多的值。我只是让他们的数字更容易理解。所以你想要任何订单上只有计数为5的项目吗?是的,对不起,我应该说得更清楚。我根本没有想到这一点。很好的解决方案。记笔记:)
const filterByOcur = (arr, numOfOccurrences) => {
// create an object to act as a counter this will let us
// iterate over the array only once
const counter = {};
const res = new Set();
for (let num of arr) {
// if it's the first time we see the num set counter.num to 1;
if (!counter[num]) counter[num] = 1;
// if counter.num is greater or equal to numOfOccurrences
// and we don't have the num in the set add it to the set
else if (++counter[num] >= numOfOccurrences && !res.has(num)) res.add(num);
}
// spread the Set into an array
return [...res];
};
console.log(
filterByOcur(a, 5)
);
const a = [1,1,1,1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5]
const counted = a.reduce((counter, value) => {
if (counter[value]) counter[value]++
else counter[value] = 1
return counter
}, {})
const uniq = Array.from(new Set(a))
const onlyOne = uniq.filter(val => counted[val] === 1)
const onlyFive = uniq.filter(val => counted[val] === 5)
const final = [].concat(onlyOne, onlyFive)