将返回对象转换为json,但只有特定的值对(convert return object to json but only certain value pairs)

编程入门 行业动态 更新时间:2024-10-25 06:32:24
将返回对象转换为json,但只有特定的值对(convert return object to json but only certain value pairs)

是的,我知道有很多关于将对象转换为json的帖子,但我的问题更具体。

说我从api调用一些数据,响应是一个看起来像这样的对象

{ date: ..., value: ..., useless-info: ..., useless-info: ... }

现在我知道我可以做这个JSON.stringify(returnedobject);

所以我得到新形成的JSON ..

{ "date": ..., "value": ..., "useless-info": ..., "useless-info": ... }

现在我想要在我新建立的json中成为"date"和"value"并且删除useless-info甚至有可能吗?

任何帮助,将不胜感激!

Yes I know there are heaps of posts about converting objects to json but my question is more specific..

Say Im calling some data from an api and the response is an object that looks like this

{ date: ..., value: ..., useless-info: ..., useless-info: ... }

now I know I can do this JSON.stringify(returnedobject);

so I get the newly formed json..

{ "date": ..., "value": ..., "useless-info": ..., "useless-info": ... }

now all I want in my newly formed json to be the "date" and "value" and remove the useless-info is this even possible?

any help would be appreciated!

最满意答案

你可以用你想要的数据创建一个新对象,或者删除你不需要的字段:

const someReturn = { date: ..., value: ..., badstuff: ... } const goodObj = { date: someReturn.date, value: someReturn.value }

或者删除字段,您可以调用delete someReturn.badstuff

You can either create a new object with the data you want, or delete the fields you don't need:

const someReturn = { date: ..., value: ..., badstuff: ... } const goodObj = { date: someReturn.date, value: someReturn.value }

Or to delete fields you can just call delete someReturn.badstuff

更多推荐

本文发布于:2023-07-29 20:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319584.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   对象   json   convert   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!