删除JavaScript中嵌套对象中的级别
我使用的REST API返回以下数据格式:删除JavaScript中嵌套对象中的级别,javascript,arrays,object,grouping,nested-object,Javascript,Arrays,Object,Grouping,Nested Object,我使用的REST API返回以下数据格式: const documents = [ { "fields": { "title": { "stringValue": "77" }, "difficulty": { "doubleValue": 77 }, }, "createTime": "2020-04-10T15:13:47.074204Z" }, { "fields":
const documents = [
{
"fields": {
"title": {
"stringValue": "77"
},
"difficulty": {
"doubleValue": 77
},
},
"createTime": "2020-04-10T15:13:47.074204Z"
},
{
"fields": {
"title": {
"stringValue": "99"
},
"difficulty": {
"doubleValue": 99
},
},
"createTime": "2020-04-10T15:13:47.074204Z"
}
]
我需要的是这种格式:
{
title: "77",
difficulty: 77
},
{
title: "99",
difficulty: 99
}
这意味着我不仅要分组这个数据,而且要完全去除中间的两个层。我该怎么做
作为奖励:我如何在这方面做得更好?有什么好的资源吗?按如下方式使用和分解:const documents=[{“字段”:{“标题”:{“stringValue”:“77”},“难度”:{“doubleValue”:77},},“createTime”:“2020-04-10T15:13:47.074204Z”,},{“字段”:{“标题”:{“stringValue”:“99”},“难度”:{“doubleValue”:99},},“createTime”:“2020-04-10T15:13:47.074204Z”};
const result=documents.map(({fields})=>({title:fields.title.stringValue,难度:fields.defestion.doubleValue}));
控制台日志(结果)按以下方式使用和分解结构进行编码>:
const documents=[{“字段”:{“标题”:{“stringValue”:“77”},“难度”:{“doubleValue”:77},},“createTime”:“2020-04-10T15:13:47.074204Z”,},{“字段”:{“标题”:{“stringValue”:“99”},“难度”:{“doubleValue”:99},},“createTime”:“2020-04-10T15:13:47.074204Z”};
const result=documents.map(({fields})=>({title:fields.title.stringValue,难度:fields.defestion.doubleValue}));
控制台日志(结果)代码>您可以使用方法和方法来实现结果
const文档=[{
“字段”:{
“标题”:{
“stringValue”:“77”
},
“困难”:{
“双重价值”:77
},
},
“createTime”:“2020-04-10T15:13:47.074204Z”
},
{
“字段”:{
“标题”:{
“stringValue”:“99”
},
“困难”:{
“双重价值”:99
},
},
“createTime”:“2020-04-10T15:13:47.074204Z”
}
]
const result=documents.map(({
字段:{
标题:t,
难度:d
}
}) => ({
标题:Object.values(t)[0],
难度:Object.values(d)[0]
}));
控制台日志(结果)代码>您可以使用方法和方法来实现结果
const文档=[{
“字段”:{
“标题”:{
“stringValue”:“77”
},
“困难”:{
“双重价值”:77
},
},
“createTime”:“2020-04-10T15:13:47.074204Z”
},
{
“字段”:{
“标题”:{
“stringValue”:“99”
},
“困难”:{
“双重价值”:99
},
},
“createTime”:“2020-04-10T15:13:47.074204Z”
}
]
const result=documents.map(({
字段:{
标题:t,
难度:d
}
}) => ({
标题:Object.values(t)[0],
难度:Object.values(d)[0]
}));
控制台日志(结果)代码>您必须遍历数据并选择字段。一种描述性的方法是使用数组上可用的map()
函数
const文档=[
{
“字段”:{
“标题”:{
“stringValue”:“77”
},
“困难”:{
“双重价值”:77
},
},
“createTime”:“2020-04-10T15:13:47.074204Z”
},
{
“字段”:{
“标题”:{
“stringValue”:“99”
},
“困难”:{
“双重价值”:99
},
},
“createTime”:“2020-04-10T15:13:47.074204Z”
}
]
让结果=documents.map(document=>({
标题:document.fields.title.stringValue,
难度:document.fields.demobility.doubleValue
}));
控制台日志(结果)代码>您必须遍历数据并选择字段。一种描述性的方法是使用数组上可用的map()
函数
const文档=[
{
“字段”:{
“标题”:{
“stringValue”:“77”
},
“困难”:{
“双重价值”:77
},
},
“createTime”:“2020-04-10T15:13:47.074204Z”
},
{
“字段”:{
“标题”:{
“stringValue”:“99”
},
“困难”:{
“双重价值”:99
},
},
“createTime”:“2020-04-10T15:13:47.074204Z”
}
]
让结果=documents.map(document=>({
标题:document.fields.title.stringValue,
难度:document.fields.demobility.doubleValue
}));
控制台日志(结果)代码>非常感谢大家-已解决。所有的解决方案都非常有效!非常感谢你们所有人-解决了。所有的解决方案都非常有效!