admin管理员组文章数量:1305969
I am trying to invert the values of a string of boolean values so that instead of
inverted([true, false, true, false, true])
it is [false, true, false, true, false]
So far I have e up with this:
function inverted(bools) {
var inversion = [];
var string= [];
while(!bools) {
string.push(bools);
}
return inversion;
}
But my code is not working, any and all help will be much appreciated!
I am trying to invert the values of a string of boolean values so that instead of
inverted([true, false, true, false, true])
it is [false, true, false, true, false]
So far I have e up with this:
function inverted(bools) {
var inversion = [];
var string= [];
while(!bools) {
string.push(bools);
}
return inversion;
}
But my code is not working, any and all help will be much appreciated!
Share Improve this question edited Mar 31, 2017 at 2:11 gyre 16.8k4 gold badges40 silver badges47 bronze badges asked Mar 31, 2017 at 1:21 lois vlois v 211 silver badge7 bronze badges 1- 1 to be frank there are obvious issues in your code - you update string variable but return inversion array in the end. Also you assume and iterate with a truthy condition. You need to iterate on the array and push an inversion on the boolean. – moha297 Commented Mar 31, 2017 at 1:31
8 Answers
Reset to default 6You can use .map()
, !
operator to invert Boolean
value, return array of results
function inverted(bools) {
return bools.map(function(bool) {return !bool})
}
function inverted(bools) {
for(var i = 0; i < bools.length; i++) {
bools[i] = !bools[i];
}
return bools;
}
Use it as
var bools = [true, false, true];
bools = inverted(bools);
Your code is just one big mistake. You do not understand basic principles of working with arrays. Do not just use my solution, please read some articles about iterating and getting an element from an array. This one should work:
function inverted(bools) {
var i, _i, inversion = [];
for(i = 0, _i = bools.length; i < _i; i++){
inversion[i] = !bools[i];
}
return inversion;
}
You can do it like this:
- Convert the string to a JavaScript array using
JSON.parse()
. - Invert all the booleans using
Array.prototype.map()
; - Convert back to a string using
JSON.stringify()
.
const booleans = "[true, false, true, false, true]";
const inverted = JSON.stringify(JSON.parse(booleans).map(b => !b));
console.log(inverted);
If by a "string", you actually mean an array, you don't need to convert to/from a string and can just do:
const booleans = [true, false, true, false, true];
const inverted = booleans.map(b => !b);
Though many answers here are already great, I thought I would chip in my "golfed" regex solution:
function inverted (b) {
return b.map(/f/.test, /f/)
}
console.log(
inverted([true, false, true, false])
)
You need to split up bools with bools.split(). Then you can check and invert them. You also are not putting anything in inversion[] prior to returning it. Something like the below should work:
function inverted(bools) {
var inversion = [];
var string= bools.split(",");
for(var i=0; i<string.length(); i++) {
inversion.push(string[i]);
}
return inversion;
}
This is another way to achieve that :
var booleans = [true, false, true, false, true];
function inverted(bools) {
var inversion = [],i=0;
while(i< bools.length) {
inversion.push(!(bools[i]));
i++;
}
return inversion;
}
var inverted = inverted(booleans);
console.log(inverted);//Outpu : [false, true, false, true, false]
let myArray = [true, false, false, true]
// single line solution:
let invertedArray = myArray.map((bool) => !bool)
console.log(invertedArray)
本文标签: Invert an array of booleans in JavaScriptStack Overflow
版权声明:本文标题:Invert an array of booleans in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741811295a2398809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论