我有以下想要通过的对象,并将特定的键提取到另一个对象中。我已经尝试了下面的代码,但是我只是对所有值都不确定。
{
"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
答案 0 :(得分:1)
您可以使用函数reduce
和处理程序中的第三个参数,即当前索引。
为了将索引分配为属性名称,可以使用称为computed-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; }
答案 1 :(得分:0)
您要查找的属性(authorTitle
和title
)位于父对象的properties
属性中,因此您必须经过properties
:< / p>
const arr = features.map(
({ properties: { authorTitle, title }}) => ({ authorTitle, title })
);
const output = { ...arr };
const 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"
}
}
];
const arr = features.map(
({ properties: { authorTitle, title }}) => ({ authorTitle, title })
);
const output = { ...arr }
console.log(output);
但是,只有一个数字属性并不稀疏的非数组对象有点奇怪,您可能会考虑仅使用.map
ped数组。
答案 2 :(得分:0)