我有多个数组,但其中一些数组的值为0
或未定义。我想要做的是,每次条件为时,将counter
值推到一个新数组中。
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.
}
在前三次运行中,只有一个数组通过了条件1、1、1,但是在第4和第5次运行时,两个数组都通过了测试,因此接下来的两个条目应该是2,2。
所以,我想要实现的是这样一个新的数组-- [1, 1, 1, 2, 2]
。
注意:数组的数目不是两个。还有许多其他的,都是相同长度的,它们的数量(数组的数字)可以随着时间的推移而增长,以及每个单独的条目中的条目数,但是到最后,它们都将具有相同的长度。
也许是其他类型的循环?有什么建议吗?
发布于 2018-06-12 13:35:39
我认为在这里最好的做法是在测试了两个数组的真实性之后,将这两个数组合并成一个数组:
let array1 = [5, 6, 44, 2, 6];
let array2 = ['', '', '', 5 , 7];
const result = array1.map((item1, i) => {
const item2 = array2[i];
return Boolean(item1) + Boolean(item2); // gets converted to number
});
console.log(result);
当然,这是假设数组有相同数量的元素。如果没有,您可以首先对两个数组的长度使用Math.max
。
如果有多个数组,首先将它们放在一个数组中,然后遍历该数组:
const arrs = [
[5, 6, 44, 2, 6],
['', '', '', 5 , 7],
];
const { length } = arrs[0];
const result = Array.from(
{ length },
(_, i) => arrs.reduce((a, arr) => a + Boolean(arr[i]), 0)
);
console.log(result);
或者您可能更喜欢只访问每个子数组一次:
const arrs = [
[5, 6, 44, 2, 6],
['', '', '', 5 , 7],
];
const { length } = arrs[0];
const result = [];
arrs.forEach(arr =>
arr.forEach((item, i) => result[i] = (result[i] || 0) + Boolean(item))
);
console.log(result);
发布于 2018-06-14 13:55:20
一种方法可以是为每个要计数的位置创建一个新的数组。这使得计算原始数组中每个位置的真实值的数量变得很容易。
使用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]
如果您想运行这个脚本并查看结果,可以在https://runkit.com/amiel/5b22dffb85fb2d00128e2217上找到它。
发布于 2018-06-12 14:36:43
@Ray,假设两个数组的长度相同,那么下面的代码将按照您的意愿工作。
在这里,使用三值运算符(条件运算符) ?:
和两个逻辑运算符&&
,||
对0
、''
以及undefined
、null
都发挥了很大的roles.The代码的作用。
// empNum() is a function which takes 2 arrays of same length as parameters
var empNum = (data1, data2) => {
var activeArr = [];
for (var i=0; i<data1.length; i++) {
activeArr.push(data1[i] && data2[i] ? 2 : 1); // Using ternary operator ?:
}
return activeArr;
}
// Test 1
var array1= [5, 6, 44, 2, 6];
var array2 = ['', '', '', 5, 7];
console.log(empNum(array1, array2)); //[ 1, 1, 1, 2, 2 ]
// Test 2
console.log(empNum([5, 0, 44, 2, 6], [8, 7, undefined, 5, 7,] )); // [ 2, 1, 1, 2, 2 ]
// Test 3
console.log(empNum(["JavaScript", 0, undefined, null, 6], [8, 7, undefined, 5, 7,] )); // [ 2, 1, 1, 1, 2 ]
// Test 4
console.log(empNum([0, 0, 44, 2, "Python"], [8, 7, undefined, 5, 7,] )); // [ 1, 1, 1, 2, 2 ]
产出
[ 1, 1, 1, 2, 2 ]
[ 2, 1, 1, 2, 2 ]
[ 2, 1, 1, 1, 2 ]
[ 1, 1, 1, 2, 2 ]
https://stackoverflow.com/questions/50826088
复制相似问题