Javascript method returns array of object instead of object.
Helper method:
export const getDefaultValue = (valueList) => {
return defaultValue = valueList.map((item, index) => {
if (item.isDefault) {
return item;
}
});
}
Input:
let valueList = [{itemId: 1, isDefault: true}, {itemId: 2, isDefault: false}, {itemId: 3, isDefault: false}]
When calling getDefaultValue(valueList), I'm getting below output:
Output:
[{itemId: 1, isDefault: true}, undefined, undefined]
Shouldn't getDefaultValue return only {itemId: 1, isDefault: true}?
Newbie to JavaScript. What am I missing here?
Array.prototype.map(): "creates a new array with the results of calling a provided function on every element in the calling array." - Should have been part of your research.isDefaultis truthy?.mapcreates a new array and won't stop on the first occurrence. You probably wanted to usefilterinstead, or, even better,.find