简体   繁体   English

如何显示嵌套在数组中的对象的匹配描述?

[英]How to display matching description of an object nested in an array?

I have a function, whose purpose is to display specific description of a particular object that is nested within an array.我有一个函数,其目的是显示嵌套在数组中的特定对象的特定描述。 The idea is that once the function ( findSettings() ) receives a particular array( systemSettings ) and some key( tab12 ) that is in the objects, it should run through the switch statement and provide its matching description.这个想法是,一旦函数( findSettings() )接收到一个特定的数组( systemSettings )和对象中的一些键( tab12 ),它应该运行 switch 语句并提供其匹配的描述。

In other words, if the function's argument is 'tab12', then it should return the description, 'Description for tab12'.换句话说,如果函数的参数是“tab12”,那么它应该返回描述,“tab12 的描述”。

I have tried to find a matching object using the find method, and that works well, although if I try to run the switch statement, it returns an error: 'Obejct is possibly undefined'.我尝试使用 find 方法查找匹配的对象,并且效果很好,但如果我尝试运行 switch 语句,它会返回一个错误:“Obejct 可能未定义”。

const systemSettings = [
  {key: 'tab1', value: 'Main Tab'}, 
  {key: 'tab12', value: 'Tab 12'}, 
  {key: 'tab13', value: 'Tab 13'}, 
  {key: 'tab4', value: 'Tab 4'}
]

type sampObj = {
  key: string;
  value: string;
}

let info: string = '';

function findSetting(arr: sampObj[], settingKey: string) {

  const selectedObjs = arr.find(obj => obj.key === settingKey);

  switch(selectedObjs.key) {
    case 'tab1':
      info += 'Description for tab1';
      break;
    case 'tab12':
      info += 'Description for tab12';
      break;
    case 'tab13':
      info += 'Description for tab13';
      break;
    case 'tab4':
      info += 'Description for tab4';
      break;
    default: 
      info += 'No description available'
  }

}

findSetting(systemSettings, 'tab12')```

Any assistance would be highly appreciated.
Thanks.

const selectedObjs = arr.find(obj => obj.key === settingKey); could return undefined if an object with the specified key is not in the array.如果具有指定键的对象不在数组中,则可能返回undefined

You can check to see if the object is not undefined before executing your switch statement:您可以在执行 switch 语句之前检查对象是否未定义:

if(selectedObjs) {
   switch(selectedObjs.key) {
       case 'tab1':
          info += 'Description for tab1';
          break;
        case 'tab12':
          info += 'Description for tab12';
          break;
        case 'tab13':
          info += 'Description for tab13';
          break;
        case 'tab4':
          info += 'Description for tab4';
          break;
        default: 
          info += 'No description available'
  }
}
else {
   // Handle invalid settingKey case
}

I think simpler solution would be to use something like Map for storing your descriptions against the keys.我认为更简单的解决方案是使用Map 之类的东西来存储您对键的描述。 Also, there is handy Utility Type in TypeScript called Record for type safety.此外,TypeScript 中有一个名为Record 的方便的实用程序类型,用于类型安全。 Then you simply don't have to provide your array in findSetting function然后你根本不需要在findSetting函数中提供你的数组

  systemSettingsDescriptions: Record<string, string> = {
    tab1: "Description for tab1",
    tab2: "Description for tab2",
    tab3: "Description for tab3"
  };

  systemSettings: sampObj [] = [
    { key: "tab1", value: "Main Tab" },
    { key: "tab12", value: "Tab 12" },
    { key: "tab13", value: "Tab 13" },
    { key: "tab4", value: "Tab 4" }
  ];

  function findSetting(settingKey: string): string {
    this.systemSettingsDescriptions[settingKey] || "No description available";
  }

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

相关问题 查找并突出显示单词中所有出现的单词并显示匹配的描述 - Find and highlight all occurrences of a word from an array and display matching description 如何将数组推送到仅匹配父属性的嵌套子对象? - How to push array to nested child object of only matching parent properties? 使用 JavaScript 将嵌套数组内的变量与 object 匹配 - Matching a variable with an object inside a nested array with JavaScript 如何排列对象数据的嵌套数组以进行反应表组件显示 - How to arrange nested array of object data for react table component display 将嵌套数组转换为对象,将数组索引与对象ID匹配 - Converting nested array to object, matching array index with object id 如何在模板中显示嵌套数组 - How to display a nested array in a template 迭代嵌套的对象数组,查找 id 并更新与 id 匹配的对象 - Iterate nested array of objects, find id and update object matching the id 过滤嵌套的对象数组以返回整个匹配的对象 - Filtering on nested array of objects to return the entire matching object 根据嵌套数组中的匹配键值获取对象 - Get object based on matching key value in nested array 如何将 Array 嵌套对象转换为 Array 对象? - how to convert an Array nested object into an Array object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM