3π
β
I think what you are trying to do is to extract the type
of the object with the same name as the value of key, in that case a more appropriate solution will be to do that exactly that β ie find the element with the given name then extract its type
getPropType(key) {
console.log(this.fields)
console.log(key);
var type;
this.fields.some(field => {
if (field.name === key) {
type = field.type;
return true;
}
});
return type
}
If you want to use .map()
, then
return this.fields.filter(field => field.name === key).map(field => field.type).join('')
π€Arun P Johny
1π
Array.prototype.map() returns array. Your βgetPropTypeβ returned [βtextβ, ββ] and it should be reduced in your case.
return this.fields.map(field => {
if (field.name === key) return field.type
}).reduce(function(prev, curr, index, array){return prev + curr});
π€J.Cheong
Source:stackexchange.com