若数组和为0,如何在JavaScript中从数组中删除逻辑?
我有如下数组若数组和为0,如何在JavaScript中从数组中删除逻辑?,javascript,arrays,Javascript,Arrays,我有如下数组 [["a",0,0,0],["b",30,20,10],["c",40,50,60],["d",0,0,0],...] 若每个数组中的数字之和为零,我想移除一些数组,并得到以下返回值 [["b",30,20,10],["c",40,50,60],...] 有人能告诉我如何用JavaScript实现逻辑来实现这一点吗 通过迭代每个数组并使用reduce跟踪数组中的数字之和,过滤数组中的数组: const输入=[ [“a”,0,0,0], [“b”、30、20、10],
[["a",0,0,0],["b",30,20,10],["c",40,50,60],["d",0,0,0],...]
若每个数组中的数字之和为零,我想移除一些数组,并得到以下返回值
[["b",30,20,10],["c",40,50,60],...]
有人能告诉我如何用JavaScript实现逻辑来实现这一点吗
通过迭代每个数组并使用reduce
跟踪数组中的数字之和,过滤数组中的数组:
const输入=[
[“a”,0,0,0],
[“b”、30、20、10],
[“c”、40、50、60],
[“d”,0,0,0],
[e',30,-30]
];
console.log(
输入.过滤器((arr)=>(
arr.reduce((a,项目)=>(
项目类型==='编号'
?a+项
:a
), 0)
))
);
通过迭代每个数组并使用REDUCT
跟踪数组中的数字之和,过滤数组中的数组:
const输入=[
[“a”,0,0,0],
[“b”、30、20、10],
[“c”、40、50、60],
[“d”,0,0,0],
[e',30,-30]
];
console.log(
输入.过滤器((arr)=>(
arr.reduce((a,项目)=>(
项目类型==='编号'
?a+项
:a
), 0)
))
);
使用过滤器
方法,只返回总和不为0的嵌套数组
设x=[
[“a”,0,0,0],
[“b”、30、20、10],
[“c”、40、50、60],
[“d”,0,0,0]
];
让结果=x.filter(函数(项){
//保存和的变量
设和=0;
//因为内部数组的第一个值是string
//从索引1开始循环
对于(变量i=1;i console.log(result)
使用filter
方法,只返回总和不为0的嵌套数组
设x=[
[“a”,0,0,0],
[“b”、30、20、10],
[“c”、40、50、60],
[“d”,0,0,0]
];
让结果=x.filter(函数(项){
//保存和的变量
设和=0;
//因为内部数组的第一个值是string
//从索引1开始循环
对于(变量i=1;i console.log(结果)
使用过滤器
和减少
来计算数字总和:
const a=[
[“a”,0,0,0],
[“b”、30、20、10],
[“c”、40、50、60],
[“d”,0,0,0]
]
var filtered=a.filter(e=>!!e.reduce((a,n)=>isNaN(n)?a:a+n,0))
console.log(JSON.stringify(filtered))
使用filter
和reduce
计算数字总和:
const a=[
[“a”,0,0,0],
[“b”、30、20、10],
[“c”、40、50、60],
[“d”,0,0,0]
]
var filtered=a.filter(e=>!!e.reduce((a,n)=>isNaN(n)?a:a+n,0))
log(JSON.stringify(filtered))
有很多方法可以做到这一点。对我来说,把问题分成两部分很容易
看看这些数字加起来是否等于0
删除总和为0的数组
如果需要的话,研究一下方法和步骤
// Determine if a subarray adds up to 0.
function sumIsZero(arr) {
// You could also do
// const rest = arr.slice(1)
// If you're not familiar with rest spread operators
const [letter, ...rest] = arr;
return rest.reduce((x, y) => x + y, 0) === 0;
}
// Create a new array that only includes the subarrays that add up to 0
const filtered = arrs.filter(sumIsZero);
有很多方法可以做到这一点。对我来说,把问题分成两部分很容易
看看这些数字加起来是否等于0
删除总和为0的数组
如果需要的话,研究一下方法和步骤
// Determine if a subarray adds up to 0.
function sumIsZero(arr) {
// You could also do
// const rest = arr.slice(1)
// If you're not familiar with rest spread operators
const [letter, ...rest] = arr;
return rest.reduce((x, y) => x + y, 0) === 0;
}
// Create a new array that only includes the subarrays that add up to 0
const filtered = arrs.filter(sumIsZero);
这是哑方法:
const array = [["a",0,0,0],["b",30,20,10],["c",40,50,60],["d",0,0,0]]
var filteredArray = []
for (const subArray of array) {
var subArrayJustNumbers = subArray.slice(0) // copy the subArray
subArrayJustNumbers.shift() // remove the the first object
var sum = 0 // add all the numbers in the subarray
for (const n of subArrayJustNumbers)
sum += n
if (sum != 0) { // if the sum of all numbers isn't zero, append it to the filteredArray
filteredArray.push(subArray)
}
}
这是哑方法:
const array = [["a",0,0,0],["b",30,20,10],["c",40,50,60],["d",0,0,0]]
var filteredArray = []
for (const subArray of array) {
var subArrayJustNumbers = subArray.slice(0) // copy the subArray
subArrayJustNumbers.shift() // remove the the first object
var sum = 0 // add all the numbers in the subarray
for (const n of subArrayJustNumbers)
sum += n
if (sum != 0) { // if the sum of all numbers isn't zero, append it to the filteredArray
filteredArray.push(subArray)
}
}
你试过什么吗?你试过什么吗?