下面是我想要遍历的以下对象,如果特定的对象properties.title
与任何其他对象相同,则获取作者姓名并将其添加到新对象中(我认为将其添加到新对象中会更容易)。
business1
{
"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",
"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",
"authorTitle": "Iacopo Vagliano"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
10.14,
54.33
]
},
"place_name": "pretend place",
"properties": {
"title": "new title",
"authorTitle": "joe blogs"
}
}
],
"properties": "",
"authors": ""
}
我想要的对象(improvedObj)
var improvedObj = {
obj1 = {
title:'Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation"',
authorList: 'Florian Mai,Iacopo Vagliano'
},
obj2 = {
title:'new title',
authorList: 'joe blogs
}
}
我尝试过的
extractorArray = []
for(i=0; i<business1.features.length;i++){
extractorArray.push(business1.features[i].properties)
}
console.log('extractor', extractorArray)
var extractedValues = extractorArray.map((title) => (title));
var extractedAuthor = extractorArray.map((authorTitle) => (authorTitle))
var improvedObj = {
objList : {
title:extractedValues,
authorList: extractedAuthor
}
}
上面介绍了每个特性,获取它的属性并将其推送到extractorArray
(这样我就可以对它使用.map
函数)。现在,我的想法并不起作用,因为我只是从对象中的数组中复制(如果标题相同,就取它的作者)。
发布于 2019-03-04 07:41:03
我猜你改进的对象实际上是数组,如果我错了,请纠正我。那么下面的内容应该对你有效
const authorsByBook = k.features.map(feature => feature.properties).reduce((byTitle, feature) => {
if(!byTitle[feature.title]) byTitle[feature.title] = [];
byTitle[feature.title].push(feature.authorTitle);
return byTitle
}, {})
const improvedObject = Object.keys(authorsByBook).map(title => ({ title, authorList: authorsByBook[title].join(',')}))
第一部分循环遍历列表并创建结构为{title: authors[]}的对象。之后,基于标题将其分解为具有对象的数组{title,}
https://stackoverflow.com/questions/54974842
复制相似问题