javascript - Why is the following map function returning the strings with commas? -
i'm looping through object. if 1 of keys matches value of fields
object return value of type
of fields
object:
// objects user: { email: '', password: '' } formfields: [ { name: 'email', type: 'text', required: true }, { name: 'password', type: 'password', required: true } ] // html <input :type="getproptype($key)" // function getproptype (key) { console.log(this.fields) console.log(key) return this.fields.map(field => { if (field.name === key) return field.type }) }
it works except comma returned every field.type
:
which strange since logs don't output commas:
what cause?
i think trying extract type
of object same name value of key, in case more appropriate solution - ie find element given name extract 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 want use .map()
, then
return this.fields.filter(field => field.name === key).map(field => field.type).join('')
Comments
Post a Comment