繁体   English   中英

数组值中的对象名称

[英]Object name from array value

我有一些值的数组:

let typeArray = ["name", "strret", "car", "type"];

我有一个对象:

let formDefinitionObject = {schema:{}}

我希望formDefinitionObject对象是这样的:

let formDefinitionObject = {
  schema: {
    name: {
      type: 'string',
      title: 'Name',
      required: true
    },
    strret: {
      type: 'string',
      title: 'Strret'
    },
    car: {
      type: 'string',
      title: 'Car'
    },
    type: {
      type: 'string',
      title: 'Type'
    }
  }
}

我希望数组中的每个项目动态地成为formDefinitionObject.schema对象中的对象。 例如,如果我在数组typeArray.push('country')再添加一项以在 formDefinitionObject.schema 对象中自动添加此对象。

无法理解如何required: true适合。其余的事情可以按如下方式完成

 var getFormDefinition = function(arr) { function getSchemaObj(arr) { return arr.map(d => ({ [d]: { type: typeof(d), title: d } })) .reduce((a, b) => ({ ...a, ...b })) } var schemaObj = getSchemaObj(arr) arr.push = function(...d) { Object.assign(schemaObj, getSchemaObj(d)) return Array.prototype.push.call(arr, ...d) } return ({ schema: schemaObj }) } var typeArray = ["name", "strret", "car", "type"]; var result = getFormDefinition(typeArray) console.log(result) typeArray.push('nitish') console.log(result)


您可以在带有set陷阱的typeArray上使用Proxy ,因此每次将新值推送到代理数组时,您还可以向架构添加新属性。 这样你就可以模拟观察者模式。

您还可以创建一些模式来添加其他属性,例如requiredname:prop1:prop2但这种方式值固定为true

 let typeArray = ["name:required", "strret", "car", "type"]; let formDefinitionObject = { schema: {} } let proxyArray = new Proxy(typeArray, { set(obj, prop, value) { if (prop != 'length') addToSchema(formDefinitionObject.schema, value); return Reflect.set(...arguments); } }) function capitalize(string) { return string.charAt(0).toUpperCase() + string.slice(1); } function addToSchema(schema, prop) { const [name, ...params] = prop.split(':'); schema[name] = { type: 'string', title: capitalize(name) } params.forEach(param => schema[name][param] = true); return schema; } proxyArray.reduce(addToSchema, formDefinitionObject.schema); proxyArray.push('email:required:isEmail'); proxyArray.push('phone'); console.log(formDefinitionObject)

更新:您可以使用类似name:prop1|value:prop2来添加除true之外的属性值,但如果您未指定值,则默认值仍然为true

 let typeArray = ["name:required", "strret", "car", "type"]; let formDefinitionObject = { schema: {} } let proxyArray = new Proxy(typeArray, { set(obj, prop, value) { if (prop != 'length') addToSchema(formDefinitionObject.schema, value); return Reflect.set(...arguments); } }) function capitalize(string) { return string.charAt(0).toUpperCase() + string.slice(1); } function addToSchema(schema, prop) { const [name, ...params] = prop.split(':'); schema[name] = { type: 'string', title: capitalize(name) } params.forEach(param => { const [key, value] = param.split('|'); schema[name][key] = value ? value : true }); return schema; } proxyArray.reduce(addToSchema, formDefinitionObject.schema); proxyArray.push('email:required:isEmail'); proxyArray.push('phone:default|123/555-333:required'); proxyArray.push('address') console.log(formDefinitionObject)

即使您没有详细说明如何required字段或任何字段都必须为string ,但这里有一个基于您目前提供的内容的解决方案。

下一个片段完成了这项工作,它有一些解释:

 let typeArray = ['name', 'strret', 'car', 'type'], formDefinitionObject = { schema: {} }; /** cycle through "typeArray" and populate "formDefinitionObject.schema" **/ typeArray.forEach(el => { let currObj = { type: 'string', /** YOU DID NOT SPECIY HOW TO DECIDE THE TYPE **/ title: el[0].toUpperCase() + el.substring(1), /** the title with the first letter being capitalized as you provided in the question. You can just use "el" instead of "el[0].toUpperCase() + el.substring(1)" if you'd like to print as it is **/ }; el === 'name' && (currObj['required'] = true); /** YOU DID NOT SPECIY HOW TO DECIDE IF A FIELD HAS TO BE REQUIRED. I just saw only the "name" as required so I did a basic (yet a stupid) check if the current element is "name" add a required to it **/ formDefinitionObject.schema[el] = currObj; /** add the current record to the "schema" attribute **/ }); console.dir(formDefinitionObject); /** printing the result **/

如果您在评论部分回答我们的问题,我会在这里。

到那时,希望我能把你推得更远。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM