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

Convert Javascript response to ArrayMap - Stack Overflow

programmeradmin3浏览0评论

I have the following response from a Javascript ElasticSearch Query, and i need to map it to the below structure. Is there a more efficient way to do this than what I am currently doing?

Thanks

Structure I need to map to: (about 700 of these)

[{
    "coordinates": ["48", "37"],
    "name": "something",
    "population": "501"
},

Current structure of my data being returned:

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

0: Object

 _id: "4"
 _index: "locationIndex"
 _score: 1
 _source: Object
   coordinates: Array[2]
       0: -77.080597
       1: 38.892899
       length: 2
       __proto__: Array[0]
 name: "someName"
 population: 57205
1: Object
 ...

What I'm trying but fails:

 var results= [{
                "key": 'coordinates',
                resp.coordiantes[0],
                resp.coordinates[1],
                "key": 'name',
                resp.name                    
                })
            }];

I have the following response from a Javascript ElasticSearch Query, and i need to map it to the below structure. Is there a more efficient way to do this than what I am currently doing?

Thanks

Structure I need to map to: (about 700 of these)

[{
    "coordinates": ["48", "37"],
    "name": "something",
    "population": "501"
},

Current structure of my data being returned:

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

0: Object

 _id: "4"
 _index: "locationIndex"
 _score: 1
 _source: Object
   coordinates: Array[2]
       0: -77.080597
       1: 38.892899
       length: 2
       __proto__: Array[0]
 name: "someName"
 population: 57205
1: Object
 ...

What I'm trying but fails:

 var results= [{
                "key": 'coordinates',
                resp.coordiantes[0],
                resp.coordinates[1],
                "key": 'name',
                resp.name                    
                })
            }];
Share Improve this question asked Mar 30, 2015 at 16:35 user2816352user2816352 3351 gold badge6 silver badges16 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 9

Assuming that your data is stored inside a myData variable, you can use the Array.prototype.map method to manipulate it and achieve what you want. Here's a solution:

result = myData.map(function(obj) {
    return {
        coordinates: obj._source.coordinates,
        name: obj.name,
        population: obj.population
    }
});

Simple as this! The result will be something like this:

[
    {
        "coordinates": [-77.080597, 38.892899],
        "name": "some name",
        "population": 52701
    },
    {
        "coordinates": [-54.930299, 30.992833],
        "name": "some name 2",
        "population": 84229
    },
    {
        "coordinates": [-82.001438, -5.38131],
        "name": "some name 3",
        "population": 5991
    } //, ...
]

It looks like you don't quite understand Object syntax in Javascript; in order for my answer to make the most sense, you may wish to read up a little on them.

Now that you understand Objects more, it should bee quite clear that what you want looks something like:

var results = [];
for (var i = 0, len = data.length; i < len; i++)
{
    var resp = data[i];
    results.push({
        'coordinates':resp['source']['coordinates'],
        'name':resp.name,
        'population':resp.population
    });
}

For bonus points, you could include a JS framework like jQuery and just uase a map function.

I like Marcos map solution the most but also possible and quick is to use Array.from(data). This helped me in the past to convert the response API data that should be an array but wasn't yet.

I am the author of the open source project http://www.jinqJs.. You can easily do something like this to do what you want.

var result = jinqJs().from(data5).select(function(row){
    return {coordinates: [row.coordinates[0]['0'], row.coordinates[0]['1']],
            name: row.name,
            population: row.population
           }
         });
发布评论

评论列表(0)

  1. 暂无评论