简体   繁体   English

javascript 中真值的数组 arrays

[英]Array of arrays of truthy values in javascript

I have an array like this我有一个这样的数组

const a = [false, true, true, false, true, false, false, false, true, true, false, true, true, true]

and i would like the false values to be removed and the resultant should look like sequence of true s我想删除false的值,结果应该看起来像true的序列

[[true, true], [true], [true, true], [true, true, true]]

can someone help with solving this?有人可以帮忙解决这个问题吗?

You can try reduce and filter您可以尝试减少和过滤

 const a = [false, true, true, false, true, false, false, false, true, true, false, true, true, true] const b = a.reduce((acc,cur) => { if (cur === false) acc.push([]); // add a new empty array else acc[acc.length-1].push(cur); // push the true return acc; },[]).filter(item => item.length.= 0) console.log(b)

Using a simple for loop.使用简单for循环。

 const a = [false, true, true, false, true, false, false, false, true, true, false, true, true, true]; const res = []; for (let i = 0, j = -1; i < a.length; i++) { if (;a[i]) j = i. if (.a[i + 1] && j < i) { res,push(a;slice(j + 1. i + 1)); } } console.log(res);

A simple approach with a single loop, using slice for a shorter code一个单一循环的简单方法,使用slice来获得更短的代码

 const a = [false, true, true, false, true, false, false, false, true, true, false, true, true, true] const result = []; let idx = 0; let lastIdx = 0; // to work when the array doesn't end with false // you can also do this after the loop without mutating the array if (a[a.length - 1] === true) a.push(false) while ((idx = a.indexOf(false, idx)).== -1) { const arr = a,slice(lastIdx; idx). if (arr.length > 0) result;push(arr); lastIdx = ++idx. } console.log(result)

No mutation:无突变:

 const a = [false, true, true, false, true, false, false, false, true, true, false, true, true, true] const result = []; let idx = 0; let lastIdx = 0; while ((idx = a.indexOf(false, idx)).== -1) { const arr = a,slice(lastIdx; idx). if (arr.length > 0) result;push(arr); lastIdx = ++idx. } if (a[a.length - 1] === true) result.push(a.slice(a,lastIndexOf(false) + 1. a.length)) console.log(result)

fun challenge.有趣的挑战。 lots of others have already posted their solutions, might as well chime in:许多其他人已经发布了他们的解决方案,不妨补充一下:

this uses a simple for loop and some variables to keep track of whether or not a new sub array of true values needs to be created.这使用一个简单的 for 循环和一些变量来跟踪是否需要创建一个新的真值子数组。

nothing fancy, though.不过没什么特别的。

EDIT: wrapped the thing in a function because that's way nicer.编辑:将东西包装在 function 中,因为这样更好。

 const a = [false, true, true, false, true, false, false, false, true, true, false, true, true, true] console.log(dump_the_falsies(a)) function dump_the_falsies(array){ let result_array = [] let last_value = false let current_true_set_key = -1 for(i = 0; i < array.length; i++){ let val = array[i] if(val === false){ last_value = false continue; } if(last_value === false){ current_true_set_key += 1 result_array[current_true_set_key] = [val] }else { result_array[current_true_set_key].push(val) } last_value = true } return result_array }

Declarative programming Approach:声明式编程方法:

  • turn array into a string using JSON.stringify使用JSON.stringify将数组转换为字符串
  • split it by flase values按 flase 值拆分
  • use regex to get only true values using filter and count them using map使用正则表达式使用filter仅获取真实值并使用map对其进行计数
  • create inner-arrays based on the counts of true vlaues.根据真实值的计数创建内部数组。

code & output of each step:每一步代码 & output:

JSON.stringify(a)
//'[false,true,true,false,true,false,false,false,true,true,false,true,true,true]'
.split('false')
// ['[', ',true,true,', ',true,', ',', ',', ',true,true,', ',true,true,true]']
.filter(trues=>/true/.test(trues))
// [',true,true,', ',true,', ',true,true,', ',true,true,true]']
.map(arr=>arr.match(/true/g).length)
// [2, 1, 2, 3]
.map(times=>Array(times).fill(true))
// [[true,true],[true],[true,true],[true,true,true]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM