在JavaScript中,如何重命名一个大对象的几个属性?

huangapple go评论74阅读模式
英文:

In javascript, how to rename few properties of a huge object

问题

我有一个拥有120多个字段的对象,我正在寻找一种将该对象转换为新对象的方法。

新对象与原始对象大部分相同,只是有一些字段被重命名,另一些字段从毫秒时间转换为Date对象。

原始对象:
type: Record<string, unknown>
示例值:

{
  "id": 12,
  ...
  "created_at": 1577999390226497 // 毫秒时间
}

新对象:
type: Custom Object

export class NewDto {
  client_id: number;
  ...
  client_created_at: Date;
}

我尝试使用切片,但它没有起作用。示例代码:

const newObject = {
  ...originalObject,
  id: client_id,
  created_at: convertToDate(created_at)
} as NewDto;
英文:

I have an object with 120+ fields, and I am looking for a way to transform the object into a new object.

The new object is mostly identical to the original, except few fields are renamed and few fields are converted to Date object from milliseconds time.

Original Object:
type: Record&lt;string, unknown&gt;
Sample Value:

{
  &quot;id&quot;:12,
  ...
  &quot;created_at&quot;:1577999390226497 // Time in milliseconds
}

New Object
type: Custom Object

export class NewDto {
  client_id: number;
  ...
  client_created_at: Date;
  
}

I tried using slicing but it's not working. Sample code:

  const newObject = {
    ...originalObject,
    id: client_id,
    created_at: convertToDate(created_at)
  } as NewDto;

答案1

得分: 1

你应该使用解构赋值来重命名属性。但是,没有快捷方式可以将函数应用于属性值;你必须手动执行这个操作。

const {id: client_id, ...rest} = originalObject;
const res: NewDto = {...rest, client_id, client_created_at: 
                          convertToDate(originalObject.created_at)};
英文:

You should use destructuring assignment to rename a property. There is no shorthand for applying a function to a property value though; you have to do that manually.

const {id: client_id, ...rest} = originalObject;
const res: NewDto = {...rest, client_id, client_created_at: 
                          convertToDate(originalObject.created_at)};

答案2

得分: 0

鉴于您只有少量更改,您可以直接克隆整个对象,然后进行简单的修改。

function transformed(orig) {
    // 克隆原始对象。
    let cloned = { ...orig };
    // 添加/覆盖任何更改
    cloned.client_id = orig.id;
    cloned.created_at = new Date(orig.created_at/1000);
    // 删除任何重命名的字段
    delete cloned.id;
    return cloned;
}
英文:

Given that you have few changes, you can just clone the whole object and then make simple modifications.

function transformed(orig) {
	// Clone the original.
    let cloned={...orig};
    // Add/Overwrite any changes
    cloned.client_id=orig.id;
    cloned.created_at=new Date(orig.created_at/1000);
    // Remove any renamed fields
    delete cloned.id;
    return cloned;
}

答案3

得分: -1

你提供的代码接近实现所需的转换,但有一些需要解决的问题。以下是应该能工作的代码的更新版本:

const newObject: NewDto = {
  client_id: originalObject.id as number,
  ...originalObject,
  client_created_at: new Date(originalObject.created_at as number),
};

在这段代码中,我们明确指定了 newObject 的类型为 NewDto,以确保类型安全。然后,我们通过将 originalObject.id 强制类型转换为 number 来赋值 client_id 字段,因为 originalObject.id 的类型是 unknown。接下来,我们使用展开运算符 (...) 将所有字段从 originalObject 复制到 newObject。

最后,我们通过使用 Date 构造函数将毫秒级时间戳转换为 Date 对象,并将其赋给 client_created_at 字段。

确保你有必要的导入,并且 NewDto 类被正确定义。使用这段代码,你应该能够将原始对象转换为具有所需字段重命名和转换为 Date 对象的新对象。

英文:

The code you provided is close to achieving the desired transformation, but there are a few issues that need to be addressed. Here's an updated version of the code that should work:

const newObject: NewDto = {
  client_id: originalObject.id as number,
  ...originalObject,
  client_created_at: new Date(originalObject.created_at as number),
};

In this code, we explicitly specify the type of newObject as NewDto to ensure type safety. Then, we assign the client_id field by casting originalObject.id to number since originalObject.id is of type unknown. Next, we use the spread operator (...) to copy all the fields from originalObject to newObject.

Finally, we assign the client_created_at field by converting the millisecond timestamp to a Date object using the Date constructor.

Make sure you have the necessary imports and that the NewDto class is defined properly. With this code, you should be able to transform your original object into the new object with the desired field renaming and conversion to a Date object.

huangapple
  • 本文由 发表于 2023年5月28日 01:30:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定