简体   繁体   English

如何将对象转换为 Typescript 中的列表

[英]How to convert object to list in Typescript

I want to convert my object to list while passing as param to my API.我想将我的对象转换为列表,同时作为参数传递给我的 API。 I am not sure how to achieve this.我不确定如何实现这一目标。 I tried Object.keys but not getting expected output.我试过 Object.keys 但没有得到预期的输出。

Object.keys(myobj).map(function(key){
---logic
});

I am receiving list of objects from API:我从 API 接收对象列表:

[{Name : "sachin",Age  : 30},{Name : "Ramesh",Age  : 35}]

In Angular, when I use find method it is converted as object from the above list and it looks like below.在 Angular 中,当我使用 find 方法时,它被转换为上面列表中的对象,如下所示。

 {Name : "sachin",Age  : 30}

Now I have to change Age property value as 42 and pass it to API as list.现在我必须将 Age 属性值更改为 42 并将其作为列表传递给 API。

[{Name : "sachin",Age  : 42}]

Can someone help me to achieve this result.有人可以帮助我实现这个结果。

You can use this:你可以使用这个:

var payload = [{...prevObject,...{
  Age: 42
}}];

prevObject is object returned from find method. prevObject是从find方法返回的对象。

Basically, the ... (spread operator) is used to merge the two objects into one.基本上, ... (扩展运算符)用于将两个对象合并为一个。

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

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