Home > Software design >  how to get specific column's data as list from object in javascript?
how to get specific column's data as list from object in javascript?

Time:08-24

suppose we have the following json data

var example = [{
    latitude: 11.1111,
    longitude: 111.111,
    name: "for example1",
    altitude: 88
},
{
    latitude: 22.2222,
    longitude: 222.222,
    name: "for example2",
    altitude: 89
},
{
    latitude: 33.3333,
    longitude: 333.333,
    name: "for example3",
    altitude: 90
}
]

I want to get the lists(or object) that has only latitude and longitude data, because Map API in my
react-native application require them. and by some performance issues, I can't use loop.
I know that writing a separate query(MySQL) can solve this problem, but I don't think this is good solution. (If not, I will very grateful if you can tell me)


thank you for reading this, how can I solve this?

CodePudding user response:

I think you need this. it returns a new array with only lat/long items in objects

const newArray = example.map(el => {
    return {
        latitude: el.latitude,
        longitude: el.longitude
    }  
})

CodePudding user response:

example.map(item => ('latitude' && 'longitude') in item && item);

CodePudding user response:

example.map(({latitude, longitude}) => ({latitude, longitude}))

because Map API in my react-native application require them.

Would it be satisfied with an iterator?

[edit]

const projected = ( function* () {
  for (record of example) yield {
    latitude:  record.latitude,
    longitude: record.longitude,
  };
})()

CodePudding user response:

I think you are trying to save all filtered data to your database. Am I right? If it is then you can map or loop the data first in this way ->

const filterdData = example.map(({latitude, longitude}) => ({latitude, longitude}));

And then insert bulk to MySQL database. Like this way-

INSERT INTO tbl_name(col1, col2, col3) VALUES(val1, val2, val3), (val4, val5, val6), (val7, val8, val9);

  • Related