-1

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?

4
  • From the documentation on 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. Commented Aug 5, 2019 at 6:35
  • do you want only one element or all which where isDefault is truthy? Commented Aug 5, 2019 at 6:37
  • Only the element. Commented Aug 5, 2019 at 6:43
  • .map creates a new array and won't stop on the first occurrence. You probably wanted to use filter instead, or, even better, .find Commented Aug 5, 2019 at 6:44

4 Answers 4

3

map isn't right for this - use find:

export const getDefaultValue = (valueList) => {
    return defaultValue = valueList.find((item, index) => {
        if (item.isDefault) {
            return true;
        }
    });
}

You could also simplify it:

export const getDefaultValue = valueList => valueList.find(({ isDefault }) => isDefault);
Sign up to request clarification or add additional context in comments.

4 Comments

find is only find the first item. did you mean filter?
Nope. Look at the expected output.
yep. wants an object not an array with isDefault:true criteria.
And that's exactly what my answer does @MuratAras - your point is?
2

Since you just want to get the one matching value from the array, you should use .find instead:

export const getDefaultValue = (valueList) => {
    return defaultValue = valueList.find((item) => {
        if (item.isDefault) {
            return true;
        }
    });
};

.map will always construct a new array, based on all items of the old array, which isn't what you want here.

Comments

2

You need to filter the array with Array#filter and this returns either the element or not, depending on the return value.

return defaultValue = valueList.filter(item => item.isDefault);

Array#map returns a new element for each element.


After a clear problem description, you need to take Array#find for getting a single object.

This method returnd the first found element or undefined if not element fullfills the condition.

return defaultValue = valueList.find(item => item.isDefault);

Comments

0

The Array.Map() method returns array of values. Since you are returning item itself which is an object, the final output becomes array of object.

try this:

 export const getDefaultValue = (valueList) => {
    return defaultValue = valueList.map((item, index) => {
        if (item.isDefault) {
            return item.itemId;
        }
    });
}

which will also capture undefined if nothing gets returned. You can you filter method instead.

const  getDefaultValue = valueList.filter(item => item.isDefault);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.