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

时间:2019-03-10 23:14:25

标签: javascript

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

{
"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

3 个答案:

答案 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)

您要查找的属性(authorTitletitle)位于父对象的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)

我使用了地图javascript函数来提取作者和标题

查看代码:

https://stackblitz.com/edit/js-hebvsu?embed=1&file=index.js