3

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                    
                })
            }];

4 Answers 4

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
    } //, ...
]
Sign up to request clarification or add additional context in comments.

Comments

1

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 become 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.

3 Comments

Actually the native JavaScript Aarray object already has a .map method.
It depends on browser support. I still have to deal with companies that use IE8, which, as an example, doesn't support [].map(). :)
I usually tend to drop support to IE, but you can always define it by yourself as a fallback: if (!Array.prototype.map) Array.prototype.map = function(callback) { for (var i=0; i < this.length; i++) this[i] = callback(this[i]); };
1

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.

Comments

0

I am the author of the open source project http://www.jinqJs.com. 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
           }
         });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.