最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Map dynamic key to values in Javascript array - Stack Overflow

programmeradmin0浏览0评论

I need to map one object array into this format:

             "Brandenburg Gate, Berlin": {latitude: 52.516272, longitude: 13.377722},
             "Dortmund U-Tower": {latitude: 51.515, longitude: 7.453619},
             "London Eye": {latitude: 51.503333, longitude: -0.119722},
             "Kremlin, Moscow": {latitude: 55.751667, longitude: 37.617778},
             "Eiffel Tower, Paris": {latitude: 48.8583, longitude: 2.2945},
             "Riksdag building, Stockholm": {latitude: 59.3275, longitude: 18.0675},
             "Royal Palace, Oslo": {latitude: 59.916911, longitude: 10.727567}
             }

where my source array looks like

{place: "Brandenburg Gate, Berlin", latitude: 52.5, longitude 13.3   }

I can't figure out how to make place be the key of the array.

_.map(a, function (m) {return m.place: {longitude: m.longitude}})

is obviously wrong.

I need to map one object array into this format:

             "Brandenburg Gate, Berlin": {latitude: 52.516272, longitude: 13.377722},
             "Dortmund U-Tower": {latitude: 51.515, longitude: 7.453619},
             "London Eye": {latitude: 51.503333, longitude: -0.119722},
             "Kremlin, Moscow": {latitude: 55.751667, longitude: 37.617778},
             "Eiffel Tower, Paris": {latitude: 48.8583, longitude: 2.2945},
             "Riksdag building, Stockholm": {latitude: 59.3275, longitude: 18.0675},
             "Royal Palace, Oslo": {latitude: 59.916911, longitude: 10.727567}
             }

where my source array looks like

{place: "Brandenburg Gate, Berlin", latitude: 52.5, longitude 13.3   }

I can't figure out how to make place be the key of the array.

_.map(a, function (m) {return m.place: {longitude: m.longitude}})

is obviously wrong.

Share Improve this question edited Jun 9, 2015 at 2:06 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Jun 9, 2015 at 2:02 metalaureatemetalaureate 7,74211 gold badges59 silver badges97 bronze badges 2
  • Are you sure that's an array? It looks like JSON to me – beautifulcoder Commented Jun 9, 2015 at 2:09
  • @beautifulcoder: Definitely not JSON, and how the data was received/encoded is irrelevant to the problem. – Felix Kling Commented Jun 9, 2015 at 2:12
Add a ment  | 

5 Answers 5

Reset to default 2

Iterate over the array, add each value to the object and delete the placeproperty:

var obj = {};
arr.forEach(function(value) {
    obj[value.place] = value;
    delete value.place;
});

Try:

var result = {};
a.forEach(function (item) {
   result[item.place] = {longitude: item.longitude, latitude: item.latitude};
});

What is the expected result?

Maybe:

_.map(a, function (m) {
  return {"place":m.place, "longitude": m.longitude}
})

I assume you have an array of objects? If so, this will work (assuming you are using underscorejs based on _.map()):

var locationObject = {};
_(arrayOfLocations).each(function(location) {
  locationObject[location.place] = { latitude: location.latitude, longitude: location.longitude };
});

This will give you an object where the key is the place and the associated values are themselves objects containing the latitude and longitude.

You might try using obj.map() and re-map all the elements.

Here is an example:

<script type="text/javascript">
function experiment() {
var obj = [{place: "Brandenburg Gate, Berlin", latitude: 52.5, longitude: 13.3},
       {place: "Dortmund U-Tower", latitude: 51.515, longitude: 7.453619},
       {place: "London Eye", latitude: 51.503333, longitude: -0.119722}];

/* Mapped Output
"Brandenburg Gate, Berlin": {latitude: 52.516272, longitude: 13.377722},
"Dortmund U-Tower": {latitude: 51.515, longitude: 7.453619},
"London Eye": {latitude: 51.503333, longitude: -0.119722},
*/

// create a new object array
var reformattedArray = obj.map(function(o){ 
    var rObj = {};
    rObj[o.place] = {latitude : o.latitude, longitude : o.longitude};
    return rObj;
});
}
window.onload = experiment;
</script>
发布评论

评论列表(0)

  1. 暂无评论