0

I need to get string / title from last product in my array but only after I filter all my items to get only active products, and I've tried something like this:

function getProductNoteTitle() {
if (data.product && data.product.length>0) {
  var result = data.product.filter(
    item => item.productStatusId === ProductStatusEnum.Active
  );
  result.length > 0 ? return([result.length - 1].title) : return 'Not Set';
 }
}

But this wont work because I got an error on this line:

result.length > 0 ? return([result.length - 1].title) : return 'Not Set';

which says Expression expected.

2
  • 1
    return is not keyword that forms an expression, you cannot use it as the operand of something else. You want to return (… ? … : …); instead - notice the return …; being a statement. Commented Nov 28, 2019 at 22:46
  • @Bergi True mate! Thanks a lot for this nice explanation! Commented Nov 28, 2019 at 22:46

1 Answer 1

1

Does the following work for you:

return result.length > 0 ? [result.length - 1].title : 'Not Set';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.