0

I've an array of customers and I want to extract only lat and long of each customer and I want to get this kinda array

latLong=[{lat:123,long:322},{lat:111,long:333},{lat:33,long:11}]

here is my array of customers and I want to extract lat longi only from each object and put them into array of objects.

  "customers": [
        {
            "id": "5",
            "aname": "Sts Customers",
            "type": "2",
            "address": "house no e 4",
            "area_id": "7",
            "cell_no": "03334488",
            "opn_type": "Debit",
            "opngbl": "2564",
            "lat": "33.7997",
            "longi": "73.04052769999998",
            "company_id": "1",
            "acct_no": ""
        },
        {
            "id": "14",
            "aname": "New Customer",
            "type": "2",
            "address": "house no 4",
            "area_id": "8",
            "cell_no": "7878",
            "opn_type": "Credit",
            "opngbl": "2541",
            "lat": "33.7997",
            "longi": "73.04052769999998",
            "company_id": "1",
            "acct_no": ""
        },
        {
            "id": "15",
            "aname": "one more customer",
            "type": "2",
            "address": "jjkhklj",
            "area_id": "8",
            "cell_no": "8876987",
            "opn_type": "Credit",
            "opngbl": "454",
            "lat": "33.7997",
            "longi": "73.04052769999998",
            "company_id": "1",
            "acct_no": ""
        }
    ],
Malik Mati
  • 19
  • 4

1 Answers1

0

You can do something like this.

const source = {
  customers: [
{
  id: '5',
  aname: 'Sts Customers',
  type: '2',
  address: 'house no e 4',
  area_id: '7',
  cell_no: '03334488',
  opn_type: 'Debit',
  opngbl: '2564',
  lat: '33.7997',
  longi: '73.04052769999998',
  company_id: '1',
  acct_no: '',
},
{
  id: '14',
  aname: 'New Customer',
  type: '2',
  address: 'house no 4',
  area_id: '8',
  cell_no: '7878',
  opn_type: 'Credit',
  opngbl: '2541',
  lat: '33.7997',
  longi: '73.04052769999998',
  company_id: '1',
  acct_no: '',
},
{
  id: '15',
  aname: 'one more customer',
  type: '2',
  address: 'jjkhklj',
  area_id: '8',
  cell_no: '8876987',
  opn_type: 'Credit',
  opngbl: '454',
  lat: '33.7997',
  longi: '73.04052769999998',
  company_id: '1',
  acct_no: '',
},
  ],
};

const result = source.customers.map(({ lat, longi: long }) => ({ lat, long }));

console.log(result);

Basically we can use map to iterate over source.customers array.

Then we destructure customer object passed to the callback to pick only lat and longi, renaming longi into long.

Finally, we return an object containing only key-value pairs we are interested in.

dave
  • 2,199
  • 1
  • 16
  • 34