const values = dataSku.map(sku => {return map.skuInfo[sku].description})
Here, there is some posibility that map.skuInfo[sku] can be null/undefined and I need to filter it. How would it be possible ?
Sounds like you want .filter. It's a higher order function like .map.
const values = dataSku
.filter(item => item.description !== undefined)
I'm not sure your exact data structure but check it out here! It filters out all non-truthy return values.
You can something like as under
const filterNotNullAndUndefined = (sku) => (sku?.description !== null && sku?.description !== undefined);
const filterNotNUllAndUndefinedAndNonEmpty = (sku) => (sku?.description)
const sku = [
{id: 1, 'description': '1'},
{id: 2, 'description': null},
{id: 3, 'description': undefined},
{id: 4, 'description': ''},
null,
undefined
];
const result1 = sku?.filter(filterNotNUllAndUndefinedAndNonEmpty);
const result2 = sku?.filter(filterNotNullAndUndefined);
console.log(result1);
console.log(result2);
filterNotNullAndUndefined could be shorter: sku => sku?.description ?? falseNullish coalescing operator (??)
ifstatement?Array.prototype.filter()(link: MDN) - e.g.const myFilteredArray = myArray.filter(Boolean)(don't necessarily useBooleanlike I show but provide an actual comparison, even though that works filtering out "falsy" values)undefined?Array.filter()instead ofArray.map()?