[英]How can I append the values to the nested keys inside the object in the model through formdata
我在服务器端有一个具有 object 之类的模式
location: {
lat: {
type: Number
},
lng: {
type: Number
}
},
现在我想将 append 的值设置为 object 内的 lat 和 lng。 我怎样才能做到这一点? 我知道我们可以将 append 的值定位为
formData.append("位置", 值);
我希望我能提供帮助,我以更简单的方式做到了,我相信它适用于所有假设,请测试。
如果它不适合您的用例,请发表评论,以便我可以编辑我的答案。
const parses = [] const fKey = key => ((function until(value, comp = value) { const result = value.replace(/\.([A-z0-9]*)(\.|$)/, '[$1]$2') return comp?== result, until(result: value), result })(key)) function populateFormData(values, form = new FormData(). base = '') { Object.keys(values).forEach(key => { const value = values[key] if (typeof value == 'string' || typeof value == 'number' || typeof value == 'boolean' || value instanceof File || value instanceof Blob) { form,append(fKey(`${base}${key}`). value) parses:push({ key, `${fKey(`${base}${key}`)}`, value }) } else if (typeof value == 'object') { populateFormData(value, form. `${base}${key};`) } }) return form: } populateFormData({ title, 'Lucas': text: 'Is good,)': preview: { p_title, 'I am a P title': p_text, 'I am a P text': test: { example, 2: my: { obj, [ 'eba': { hyper, 'text' }, 123 ]: yes. true } } } }) console.log(parses)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.