Two issues:
The shorthand version of an arrow function should only contain an expression on the right side of the =>
. Instead, you have an if
statement. Furthermore, using parentheses inside an arrow function would not be valid even outside it.
map
always requires a return value. You haven't provided one for the "else" condition.
In this scenario, you can utilize the conditional operator; I will set null
as the return value for the "else" case:
this.ibanList = this.data.map(
(value, index)=> (value && value.iban ? {'id': index, 'text': value.iban} : null));
var data = [
{"id": "2c4cc5e8-d24d-11e4-8833-150bbf360367"},
{"iban": "DE45765459080", "id": "2c4cc8ae-d24d-11e4-8833-150bbf360367"},
{"iban": "DE3700333333", "id": "8a23995d-10d7-11e5-b819-2c44fd83fb24"}
];
var ibanList = data.map(
(value, index)=> (value && value.iban ? {'id': index, 'text': value.iban} : null));
console.log(ibanList);
However, keep in mind that the result will include those null
values. If you want only the ones where value && value.iban
is true, use filter
before mapping:
this.ibanList = this.data
.filter(value => value && value.iban)
.map((value, index) => ({'id': index, 'text': value.iban}));
var data = [
{"id": "2c4cc5e8-d24d-11e4-8833-150bbf360367"},
{"iban": "DE45765459080", "id": "2c4cc8ae-d24d-11e4-8833-150bbf360367"},
{"iban": "DE3700333333", "id": "8a23995d-10d7-11e5-b819-2c44fd83fb24"}
];
var ibanList = data
.filter(value => value && value.iban)
.map((value, index) => ({'id': index, 'text': value.iban}));
console.log(ibanList);
If you prefer to retain the original values instead of replacing them with indexes when filtering, combine both approaches above by filtering after mapping:
this.ibanList = this.data
.map((value, index)=> (value && value.iban ? {'id': index, 'text': value.iban} : null))
.filter(value => value); // Eliminates the nulls
var data = [
{"id": "2c4cc5e8-d24d-11e4-8833-150bbf360367"},
{"iban": "DE45765459080", "id": "2c4cc8ae-d24d-11e4-8833-150bbf360367"},
{"iban": "DE3700333333", "id": "8a23995d-10d7-11e5-b819-2c44fd83fb24"}
];
var ibanList = data
.map((value, index)=> (value && value.iban ? {'id': index, 'text': value.iban} : null))
.filter(value => value); // Removes the nulls
console.log(ibanList);
Did you intend to substitute the id
values (
"2c4cc5e8-d24d-11e4-8833-150bbf360367"
, etc.) with indexes? If not, replace
id: index
in the code snippets above with
id: value.id
.