Get help on our basic JavaScript and Algorithms Challenges. If you are posting code that is large use Gist - https://gist.github.com/ paste the link here.
const allControlIDs = steps
.map(({ hazardAndControls }) =>
hazardAndControls.map(({ controlIDs }) => controlIDs)
)
.reduce((acc, cur) => acc.concat(cur), [])
.reduce((acc, cur) => acc.concat(cur), []);
two maps... two reduce...
steps: [
{
hazardAndControls: [
{
controlID: ['id'] // got lazy to indent format this
}
]
}
]
steps
and an unknown number of hazardAndControls
const allControlIDs = steps.reduce((allControlIds, steps) => {
const hazardsAndControls = steps.hazardAndControls.reduce((hazardsAndControls, hazardAndControl) => {
hazardsAndControls.push(...hazardAndControl.controlIDs);
return hazardsAndControls;
}, []);
allControlIds.push(...hazardsAndControls);
return arrControlIds;
}, []);
arrControlIds
to allControlIds
spread
operator performance. But I degress
let someVar
someArr.forEach(() => {
// doing something to somVar over an array
});
Structure and Interpretation of Computer Programs
let favoriteMove = `{
"title": "Black Panther",
"director": "Ryan Coogler",
"year": 2018,
"imdb": 7.5
}`;
let favoriteMovie = JSON.parse(favoriteMove);
console.log(favoriteMovie);
`` instead of
'`
let favoriteMove = `{
"title": "Black Panther",
"director": "Ryan Coogler",
"year": 2018,
"imdb":7.5
}`;
$
when you're adding a variable inside. i.e const word = 'potato';
console.log(`My favorite word is ${word}`)