What would be a nice elegant functional solution for anding two boolean arrays in ES6?
const a1 = [true, false, false]
const a2 = [true, true, false]
should result in:
[true, false, false]
Use can use Array#map to iterate the 1st array, and get the value of the 2nd array using the index (the 2nd param in the callback):
const a1 = [true, false, false]
const a2 = [true, true, false]
const result = a1.map((b, i) => b && a2[i]);
console.log(result);
Related
I have this 2 dimensional array =
0: (3) [true, false, false]
1: (3) [true, true, false]
2: (3) [true, true, true]
3: (3) [false, false, false]
The position in the array represents the same in each i.e 0 = "Learner" 1 = "Manager", 2 = "ClientAdmin"
I want a new 2 dimensional array that looks like below
0: (3) ["Learner"]
1: (3) ["Learner", "Manager"]
2: (3) ["Learner", "Manager", "ClientAdmin"]
3: (3) []
I have tried
selectedAudienceMandatoryArrayText = []
this.selectedAudienceMandatoryArray.forEach( (isArray, index) => {
if (isArray[0] == true) {
this.selectedAudienceMandatoryArrayText[index].push("Learner");
}
if (isArray[1] == true) {
this.selectedAudienceMandatoryArrayText[index].push("Manager");
}
if (isArray[2] == true) {
this.selectedAudienceMandatoryArrayText[index].push("ClientAdmin");
}
}
but I get the error: Cannot read property 'push' of undefined
What is the most efficient way to do this. ES6 solutions welcome.
You could check if the flag is set, then take the value from roles with the index or return an empty array.
const
roles = ["Learner", "Manager", "ClientAdmin"],
data = [[true, false, false], [true, true, false], [true, true, true], [false, false, false]],
result = data.map(a => a.flatMap((f, i) => f ? roles[i] : []));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
selectedAudienceMandatoryArrayText = [];
this.selectedAudienceMandatoryArray.forEach(isArray => {
const roles = [];
if (isArray[0]) roles.push('Learner');
if (isArray[1]) roles.push('Manager');
if (isArray[2]) roles.push('ClientAdmin');
selectedAudienceMandatoryArrayText.push(roles);
}
You could push to a new array for each loop, and at the end, push that to the other array. This reduces having to keep track of the index for the outer array.
I am finding it hard to work out the best way to take multiple arrays of the same length and merge them into a single array of true values. So if true is in the index position, the new array should have true, else just leave false.
const array1 = [true, true, false, false, true]
const array = [true, false, false, true, false]
Output is:
[true, true, false, true, true]
You can do this with map. It will generate new array for you. Inside map there are certain arguments you can pass current value and iteration using which you will get the data from second array and then put an OR || condition to get the expected output.
const array1 = [true, true, false, false, true];
const array = [true, false, false, true, false];
const output = array1.map((val,i)=>val || array[i]);
console.log(output);
You can use map() and check any of e OR array[i] is true, here e belong to each element of array1 and array[i] means each element of array respectively.Finally, it will make a newArray after conditional checking inside the map function.
const array1 = [true, true, false, false, true];
const array = [true, false, false, true, false];
var newArray = array1.map((e, i) => Boolean(e | array[i]));
console.log(newArray)
I receive this in console
0: false
1: false
2: false
3: false
4: false
5: "scuole"
6: "scuole"
7: "scuole"
8: "scuole"
I need to remove all false and I have been trying to follow this on SO
and I tried
var scuole = [];
function bouncer(scuole) {
return scuole.filter(item => item);
}
bouncer([false, null, 0, NaN, undefined, ""]);
console.log(scuole);
But I still see false in console
Check comments for explaination.
var scuole = []
function bouncer(scuole) {
// .filter returns a new filtered array and in following case
// filtered array will return an array whos value is
// not equal to false
return scuole.filter(item => item !== false);
}
//when calling bouncer function which returns an array
// we need to store that returned array to scuole variable
// for future use.
scuole = bouncer([false, null, 0, NaN, undefined, ""]);
// expected result [ null, 0, NaN, undefined, ""]
console.log(scuole);
Array.filter returns a new array, so you would have to assign it to a new variable. Also you currently do not filter, you dont process the item. Try this:
var someArray = [true, true, false, true];
function bouncer(array) {
return array.filter(item => item !== false);
}
var otherArray = bouncer(someArray);
// expected output: [true, true, true]
Have a look at the documentation
You can use Boolean to cleanup all falsy values of an array.
const cleaned = [false, null, 0, NaN, undefined, ""].filter(Boolean);
// return "[]"
You can just use the "filter" function of JavaScript which will return you the filtered array. Please note, the filter will not update the original array hence you need to catch filtered array in a different array.
According to the snippet, I am returning the values except false. Let me know if you have any other issues or doubts.
var original_arr = [false, null, 0, NaN, undefined, ""]
var filtered_arr = original_arr.filter(function(item){return item != false})
console.log(filtered_arr);
var scuole = [false, null, 0, NaN, undefined, ""];
scuole= scuole.filter(item =>item!=false ||item!='false');
console.log(scuole);
you can do like this...
This should work:
const arr = [ false, null, NaN, 0, undefined, true, 'string', 1, 'text', 4.3, ''];
const newArr = arr.filter(e => e)
Should print out: [ true, 'string', 1, 'text', 4.3 ]
I have selectedSizeList on my state.
selectedSizeList = {1: false, 2: true, 3: true, 4: true, 5: false}
How do I convert my selectedSizeList to this array?
[2, 3, 4]
Do I have better algorithm than this one? (I assume my solution is not correct)
let array = [];
Objects.keys(selectedSizeList).maps((item) => {
if(item.value) array.push(item.value)
);
return array;
You can use array#filter to filter out all keys with true value.
const selectedSizeList = {1: false, 2: true, 3: true, 4: true, 5: false};
const result = Object
.keys(selectedSizeList)
.filter(k => selectedSizeList[k])
.map(Number);
console.log(result);
First of all get the array keys using Object.keys() and then use filter() to filter only the true values like this.
var selectedSizeList = {1: false, 2: true, 3: true, 4: true, 5: false};
var keys = Object.keys(selectedSizeList);
var filtered = keys.filter(function(key) {
return selectedSizeList[key]
});
console.log(filtered);
Try this (O(n) solution):
selectedSizeList = {1: false, 2: true, 3: true, 4: true, 5: false};
var arr = [];
for (var key in selectedSizeList) {
if(selectedSizeList[key]){
arr.push(key);
}
}
console.log(arr);
You could use Object.keys() to return an`array that you can then use Array.prototype.filter() on to return a new array.
selectedSizeList = {1: false, 2: true, 3: true, 4: true, 5: false};
let array = Object.keys(selectedSizeList).filter((item) => {
if(selectedSizeList[item]) return item;
});
//returns [2,3,4]
You can use Object.keys to get an array that you can then filter for values equal to true which will return an array of numbers as a string. You can then map through the array using Number to convert the number strings to numbers which may or may not be necessary depending on what you plan to do with this result. I put this into a function that you can re-use or at least not have your object hard coded with.
var selectedSizeList = {1: false, 2: true, 3: true, 4: true, 5: false};
// Re-usable function not tied to specific object
const filterForTrueValue = (obj) => Object
.keys(obj)
.filter(k => obj[k])
.map(Number);
console.log(filterForTrueValue(selectedSizeList));
I want to merge this array using or || operator
[[true,false,false],[false,false,false],[false,false,true]]
so that the output is
[true,false,true]
is this possible with map or reduce or similar?
Edit: Sorry for the unclear question - yes it was to vertically merge all sub arrays together. So the following input:
[[true,false,false],[false,false,false],[false,false,true],[false,false,true]]
would produce the same output:
[true,false,true]
You don't really need the || when you use some:
var array = [[true, false, false], [false, false, false], [false, false, true]];
var result = array[0].map( (_, i) => array.some(a => a[i]));
console.log(result);
You could reduce the arrays with mapping the same index values.
var array = [[true, false, false], [false, false, false], [false, false, true]],
result = array.reduce((a, b) => a.map((c, i) => b[i] || c));
console.log(result);
you can do it in the following way
let arr = [[true,false,false],[false,false,false],[false,false,true]];
arr = arr.map(function(element){
return element.reduce(function(a, b){
return (a|b);
})
})
console.log(arr);