Javascript 对于在所有迭代中都为真的每个条件,将计数器值推送到一个新数组中
我有多个数组,但其中一些数组的值为Javascript 对于在所有迭代中都为真的每个条件,将计数器值推送到一个新数组中,javascript,arrays,for-loop,Javascript,Arrays,For Loop,我有多个数组,但其中一些数组的值为0或未定义。我想做的是,每当一个条件为真时,将计数器的值压入一个新数组 let array1 = [5, 6, 44, 2, 6]; let array2 = ['', '', '', 5 , 7]; 我已经做到了这一点,但它只返回唯一的值。也就是说,让我们假设上面有两个数组,第一个数组有所有值-array1,第二个数组没有-array2(索引0、1、2为空,但索引3和4有其数值)。下面的函数正在返回-->[1,2] empNum = (data) =>
0
或未定义。我想做的是,每当一个条件为真时,将计数器的值压入一个新数组
let array1 = [5, 6, 44, 2, 6];
let array2 = ['', '', '', 5 , 7];
我已经做到了这一点,但它只返回唯一的值。也就是说,让我们假设上面有两个数组,第一个数组有所有值-array1,第二个数组没有-array2(索引0、1、2为空,但索引3和4有其数值)。下面的函数正在返回-->[1,2]
empNum = (data) => {
let activeArr = [];
let activeOnes = 0;
for (let i=0; i<data.length; i++) {
if (active[i] !== 0 && active[i] !== '') {
activeOnes ++;
activeArr.push(activeOnes);
}
}
return activeArr; // [1, 2 ] < -- This is not what I need.
}
empNum=(数据)=>{
设activeArr=[];
让activeOnes=0;
对于(设i=0;i我认为最好的做法是在测试真实性后,将两个数组映射成一个数组:
让数组1=[5,6,44,2,6];
设数组2=['','',5,7];
常量结果=数组1.map((项1,i)=>{
常数项2=阵列2[i];
返回布尔值(item1)+布尔值(item2);//转换为数字
});
console.log(result);
@Ray,假设两个数组的长度相同,那么下面的代码将按照您的需要工作
在这里,三元运算符(条件运算符)?:
和两个逻辑运算符&
,|
的使用发挥了重要作用。该代码适用于0
,'
,也适用于未定义的,null
在网上试试吧
一种方法是为每个要计数的位置创建一个新数组,这样可以很容易地计算原始数组中每个位置的真实值数量
使用lodash的zip
,这是相当简单的:
const array1 = [5, 6, 44, 2, 6];
const array2 = ['', '', '', 5 , 7];
const array3 = [1, '', 0, 1, false]
const zipped = _.zip(array1, array2, array3);
// `zipped` should now look like this, where each element
// is an array that represents all of the elements a given
// position from all of the original arrays.
// [
// [5, "", 1],
// [6, "", ""],
// [44, "", 0],
// [2, 5, 1],
// [6, 7, false],
// ]
const results = _.map(zipped, (array) => {
// lodash doesn't seem to have a `count`, but it does have `countBy`
const counts = _.countBy(array, (element) => new Boolean(element));
// Here `counts` will be an object showing how many items in the array were `true` and how many were `false`.
// For example, in the first iteration, it will look like `{ "false": 1, "true": 2 }`
// So, only return the count that is `true`.
return counts['true'];
});
// Here, the results is:
// [2, 1, 1, 3, 2]
如果您想运行该脚本并查看结果,可以在中找到该脚本。这看起来是一个很好的解决方案,但问题是,数组的数量不是两个。还有许多其他的,都是相同的长度,它们的数量(数组的数量)可以随着时间的推移而增长,以及每个条目中的条目数,但最终,它们仍然具有相同的长度。
[ 1, 1, 1, 2, 2 ]
[ 2, 1, 1, 2, 2 ]
[ 2, 1, 1, 1, 2 ]
[ 1, 1, 1, 2, 2 ]
const array1 = [5, 6, 44, 2, 6];
const array2 = ['', '', '', 5 , 7];
const array3 = [1, '', 0, 1, false]
const zipped = _.zip(array1, array2, array3);
// `zipped` should now look like this, where each element
// is an array that represents all of the elements a given
// position from all of the original arrays.
// [
// [5, "", 1],
// [6, "", ""],
// [44, "", 0],
// [2, 5, 1],
// [6, 7, false],
// ]
const results = _.map(zipped, (array) => {
// lodash doesn't seem to have a `count`, but it does have `countBy`
const counts = _.countBy(array, (element) => new Boolean(element));
// Here `counts` will be an object showing how many items in the array were `true` and how many were `false`.
// For example, in the first iteration, it will look like `{ "false": 1, "true": 2 }`
// So, only return the count that is `true`.
return counts['true'];
});
// Here, the results is:
// [2, 1, 1, 3, 2]