gpt4 book ai didi

javascript - 将大型 JSON 对象的值提取到另一个对象中

转载 作者:行者123 更新时间:2023-12-03 00:02:58 25 4
gpt4 key购买 nike

我有以下对象,我想要遍历该对象并将特定键提取到另一个对象中。我已经尝试过下面的代码,但我只是对所有值都未定义。

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-89.535,
34.3654
]
},
"place_name": "University, Mississippi, United States",
"properties": {
"title": "Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation",
"countries": "Germany",
"authorTitle": "Florian Mai"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
10.14,
54.33
]
},
"place_name": "24105, Kiel, Schleswig-Holstein, Germany",
"properties": {
"title": "Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation",
"countries": "Germany",
"authorTitle": "Iacopo Vagliano"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-89.535,
34.3654
]
},
"place_name": "University, Mississippi, United States",
"properties": {
"title": "Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation",
"countries": "Germany",
"authorTitle": "Lukas Galke"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-1.898631,
50.744119
]
},
"place_name": "University Roundabout, Bournemouth, Bournemouth, BH12 5EQ, United Kingdom",
"properties": {
"title": "An update on the 2014 report: \"Review of Recirculation Aquaculture System Technologies and their Commercial Application\"",
"countries": "United Kingdom",
"authorTitle": "David Fletcher"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-2.89,
43.29
]
},
"place_name": "48160, Derio, Bizkaia, Spain",
"properties": {
"title": "An update on the 2014 report: \"Review of Recirculation Aquaculture System Technologies and their Commercial Application\"",
"countries": "Spain",
"authorTitle": "Maddi Badiola"
}
}
]
}

我想遍历它并提取标题和作者标题,并将每个标题和作者标题添加到新的对象条目中

我尝试过的

extractedValues1 = business1.features.map(({ authorTitle, title }) => ({ authorTitle, title }));




testObject = {
object: extractedValues1
}

我想要实现的目标

testObject = {

0:{
authorTitle:'Florian Mai',
title:'Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation'
}
1:{
authorTitle:'Iacopo Vagliano'
Title:'Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation'
}
etc

最佳答案

您可以使用函数reduce和处理程序中的第三个参数,即当前索引。

为了将索引分配为属性名称,您可以使用名为compulated-property-name的功能。

extractedValues1 = business1.features.reduce((a, { authorTitle, title }, i) => 
Object.assign(a, {[i]: { authorTitle, title }}), Object.create(null));

var obj = {  "type": "FeatureCollection",  "features": [{      "type": "Feature",      "geometry": {        "type": "Point",        "coordinates": [-89.535,          34.3654        ]      },      "place_name": "University, Mississippi, United States",      "properties": {        "title": "Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation",        "countries": "Germany",        "authorTitle": "Florian Mai"      }    },    {      "type": "Feature",      "geometry": {        "type": "Point",        "coordinates": [          10.14,          54.33        ]      },      "place_name": "24105, Kiel, Schleswig-Holstein, Germany",      "properties": {        "title": "Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation",        "countries": "Germany",        "authorTitle": "Iacopo Vagliano"      }    },    {      "type": "Feature",      "geometry": {        "type": "Point",        "coordinates": [-89.535,          34.3654        ]      },      "place_name": "University, Mississippi, United States",      "properties": {        "title": "Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation",        "countries": "Germany",        "authorTitle": "Lukas Galke"      }    },    {      "type": "Feature",      "geometry": {        "type": "Point",        "coordinates": [-1.898631,          50.744119        ]      },      "place_name": "University Roundabout, Bournemouth, Bournemouth, BH12 5EQ, United Kingdom",      "properties": {        "title": "An update on the 2014 report: \"Review of Recirculation Aquaculture System Technologies and their Commercial Application\"",        "countries": "United Kingdom",        "authorTitle": "David Fletcher"      }    },    {      "type": "Feature",      "geometry": {        "type": "Point",        "coordinates": [-2.89,          43.29        ]      },      "place_name": "48160, Derio, Bizkaia, Spain",      "properties": {        "title": "An update on the 2014 report: \"Review of Recirculation Aquaculture System Technologies and their Commercial Application\"",        "countries": "Spain",        "authorTitle": "Maddi Badiola"      }    }  ]}

let extractedValues1 = obj.features.reduce((a, { properties: { authorTitle, title }} , i) =>
Object.assign(a, {[i]: { authorTitle: authorTitle, title }}), Object.create(null));

console.log(extractedValues1);
.as-console-wrapper { max-height: 100% !important; top: 0; }

没有数值

var obj = {  "type": "FeatureCollection",  "features": [{      "type": "Feature",      "geometry": {        "type": "Point",        "coordinates": [-89.535,          34.3654        ]      },      "place_name": "University, Mississippi, United States",      "properties": {        "title": "Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation",        "countries": "Germany",        "authorTitle": "Florian Mai"      }    },    {      "type": "Feature",      "geometry": {        "type": "Point",        "coordinates": [          10.14,          54.33        ]      },      "place_name": "24105, Kiel, Schleswig-Holstein, Germany",      "properties": {        "title": "Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation",        "countries": "Germany",        "authorTitle": "Iacopo Vagliano"      }    },    {      "type": "Feature",      "geometry": {        "type": "Point",        "coordinates": [-89.535,          34.3654        ]      },      "place_name": "University, Mississippi, United States",      "properties": {        "title": "Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation",        "countries": "Germany",        "authorTitle": "Lukas Galke"      }    },    {      "type": "Feature",      "geometry": {        "type": "Point",        "coordinates": [-1.898631,          50.744119        ]      },      "place_name": "University Roundabout, Bournemouth, Bournemouth, BH12 5EQ, United Kingdom",      "properties": {        "title": "An update on the 2014 report: \"Review of Recirculation Aquaculture System Technologies and their Commercial Application\"",        "countries": "United Kingdom",        "authorTitle": "David Fletcher"      }    },    {      "type": "Feature",      "geometry": {        "type": "Point",        "coordinates": [-2.89,          43.29        ]      },      "place_name": "48160, Derio, Bizkaia, Spain",      "properties": {        "title": "An update on the 2014 report: \"Review of Recirculation Aquaculture System Technologies and their Commercial Application\"",        "countries": "Spain",        "authorTitle": "Maddi Badiola"      }    }  ]}

let extractedValues1 = obj.features.map(({ properties: { authorTitle, title }}) =>
({ authorTitle: authorTitle, title }));

console.log(extractedValues1);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 将大型 JSON 对象的值提取到另一个对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55093349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com