TypeScript:在将对象作为参数传递时使用“%s”指定值

问题描述

我知道你可以这样装饰字符串:

let name = "Bob";
console.log("Hello,%s.",name) // => Outputs: "Hello,Bob."

我想知道当您无法在参数中指定键时,是否可以访问传入对象中的某些值。这是在 Jest 中进行测试时,在迭代对象数组时:

let people = [
  {
   id: 1,name: "Bob",age: 25
  },{
   id: 2,name: "Fred",age: 19
  }
];
describe.each(people)(
  `When the person is %s`,(person) => {
    it(`It should say "Hello,${person.name}`,() => {...})
  }

我正在控制台中查找要读取的记录值: "When the person is Bob > It should ..." ...但我得到了:

"When the person is {
   id: 1,age: 25
} > It should ...

为了简化我的测试输出,有没有办法在实际传入引用的对象之前使用像“%s.name”这样的装饰器?

解决方法

let people = [ // ... ].map(person => ([ person.name,person ])); // <----------- describe.each(people)( `When the person is %s`,(name,person) => { // <----------- it(`It should say "Hello,${name}`,() => {/* ... */}) }) 样式的替换没有任何智能,您需要自己提供名称。

如果你不想在测试中重复它们,你可以这样做:

require(ncdf4)
download.file('ftp://sioftp.ucsd.edu/CW3E_DataShare/Rutz_AR_Catalog/Rutz_ARCatalog_MERRA2_1981.nc',destfile = 'Rutz_ARCatalog_MERRA2_1981.nc',mode = 'wb')
temp <- nc_open('Rutz_ARCatalog_MERRA2_1981.nc')