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)
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.
This question already has answers here:
Convert Array to Object
(46 answers)
Closed 3 years ago.
I have the following array:
['2020-01-16', '2020-01-17', '2020-01-18']
I need to turn the array above into an object like this:
{
'2020-01-16': {selected: true, marked: true, selectedColor: 'blue'},
'2020-01-17': {selected: true, marked: true, selectedColor: 'blue'},
'2020-01-18': {selected: true, marked: true, selectedColor: 'blue'},
}
Is there a way I can do this?
Sure, using Array.reduce(), this is a pretty straightforward thing. The accumulator in the reduce function is simply an empty object, and each iteration through reduce, we create a new property with that array item's value as the property name, and define the object literal as the value of that property.
Hope this helps!
const myArray = ['2020-01-16', '2020-01-17', '2020-01-18'];
const myObject = myArray.reduce( (obj, item) => {
obj[item] = {selected: true, marked: true, selectedColor: 'blue'};
return obj;
}, {});
console.log(JSON.stringify(myObject) );
I would use reduce to handle this (documentation on reduce):
var arr = ['2020-01-16', '2020-01-17', '2020-01-18'];
arr.reduce(function(accumulator, val){
accumulator[val] = {selected: true, marked: true, selectedColor: 'blue'};
return accumulator;
}, {});
This will build out your object as it loops over the array. I like using reduce over forEach, when I can
Use .forEach() to iterate over all of them and build your object out.
var myKeys = ['2020-01-16', '2020-01-17', '2020-01-18'];
var myObject = {};
myKeys.forEach((key) => myObject[key] = {
selected: true,
marked: true,
selectedColor: 'blue'
});
console.log(myObject);
You'll likely need to change the logic as far as the specific data you're plugging in (for selected, marked, selectedColor), but this is the simplest approach generally.
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 ]
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);
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);