简体   繁体   English

JavaScript(TypeScript / Angular):基于先前的逻辑设置对象键/值的最短/最快方法?

[英]JavaScript (TypeScript/Angular): Shortest/Fastest method to set object keys/values based on previous logic?

I'm attempting to fill an array with objects where the objects are all the same, and the keys are determined from a predefined item. 我试图用对象都相同的对象填充数组,并且键是从预定义的项目确定的。 I know I can do this with multiple if...else, but that seems to be a long method to use and was hoping for something more condensed and/or efficient without using external libraries. 我知道我可以使用多个if ... else来做到这一点,但这似乎是一个很长的使用方法,并且希望在不使用外部库的情况下获得更简洁和/或更有效的东西。

Current method to fill array with hardcoded Objects: 使用硬编码对象填充数组的当前方法:

fillArray(arr,n) {
  while(arr.length < n) {
    arr.push({
        'x': Math.round(Math.random(),
        'name': 'item' + arr.length + 1
    });
  }
}

Example of what I'd like to achieve using an if: 我想使用if实现的示例:

fillArray(arr, n, type) {
  while(arr.length < n) {
    if ( type === 'type1' ) {
      arr.push({
        'x': Math.round(Math.random(),
        'name': 'item' + arr.length + 1
      });
    } else if ( type === 'type2') {
      arr.push({
        'x': Math.round(Math.random(),
        'y': Math.round(Math.random(),
        'name': 'item' + arr.length + 1
      });
    }
  }
}

In my application I will use this type of method with a dozen if items, and there will be similar methods as well. 在我的应用程序中,我将把这种类型的方法与十几个if项目一起使用,并且也会有类似的方法。 I do not wish to apply any static typing for these items, since my application may require object keys to be dynamically added. 希望对这些项目应用任何静态类型,因为我的应用程序可能需要动态添加对象键。

An idea I had that I can't seem to get a complete idea in my head would be somehow pre-defining an array of objects, and then using a find to somehow apply each item in the array as a type. 我的想法似乎无法完全理解,那就是预定义对象数组,然后使用查找以某种方式将数组中的每个项目都应用为类型。

Psuedo-code (untested): 伪代码(未经测试):

myTypes = [{
  type: 'type1', keys: ['x', 'name']},
  {type: 'type2', keys: ['x', 'y', 'name']
}];
fillArray(arr,n, type) {
  let currentType = this.myTypes.find( myTypes => myTypes.type === type).keys
  while(arr.length < n) {
    // add for loop to iterate over each key/value here separately
  }
}

Would this type of method work, be efficient, and is a reasonable practice to this approach? 这种方法会行之有效,是否有效,并且是该方法的合理实践吗? Are there any other standard practices for this type of array population with dynamic keys? 对于具有动态键的这种类型的阵列填充,是否还有其他标准做法?

Moving to dynamic properties with object like keys will harm type safety. 使用诸如keys类的对象移动到动态属性将损害类型安全。 Since TypeScript is a primary language, this is an important concern. 由于TypeScript是主要语言,因此这是一个重要的问题。 Also, keys doesn't allow to control property values. 另外, keys不允许控制属性值。

It can be a hierarchy of classes or some factory functions: 它可以是类的层次结构或某些工厂功能:

class Type1 {
  x = ...;
  name = ...;
}

class Type2 extends Type1 {
  y = ...;
}

const typesMap = { type1: Type1, type2: Type2 };

fillArray(arr, n, type) {
  while(arr.length < n) {
    arr.push(new typesMap[type])
  }
}

The difference from the original code is that it won't work properly if type is incorrect or missing, which may or may not be desirable. 与原始代码的不同之处在于,如果type不正确或丢失,则它将无法正常工作,这可能是不希望的。

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

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