[Vuejs]-Why is the following map function returning the strings with commas?

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

Leave a comment