Javascript 如果对象中的值与另一个对象属性匹配,如何获取这些值
下面我有一个对象,我想对其进行迭代,如果特定的object properties.title与任何其他对象相同,请使用作者名称并将其添加到一个新对象中,我认为将其添加到一个新对象中会更容易 业务1Javascript 如果对象中的值与另一个对象属性匹配,如何获取这些值,javascript,json,Javascript,Json,下面我有一个对象,我想对其进行迭代,如果特定的object properties.title与任何其他对象相同,请使用作者名称并将其添加到一个新对象中,我认为将其添加到一个新对象中会更容易 业务1 { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point"
{
"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": ""
}
我想改进的对象
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
}
}
上面介绍了每个特性,获取其属性并将其推送到提取器阵列,以便我可以在其上使用.map函数。现在我的想法不起作用了,因为我只是从对象中的数组中复制。如果标题相同,就拿它的作者来说。我猜你改进的对象实际上是数组,如果我错了,请纠正我。下面这些应该对你有用
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,}的数组,我无法确切理解您想要做什么。是否要获取具有相同名称的所有对象?或者,您只需浏览列表,检查给定一个对象A,另一个对象是否具有与A相同的属性。标题?第二个建议。我想通读一下,如果另一个对象与标题相同,则将作者附加到该对象上。并将作者的标题添加到新对象中。同时获得坐标是否会太难和太粗糙?所以在新的阵列中会有一组坐标,就像作者一样?