2

I'm having a brain fart, can someone point me in the right direction please.

Objective: Taking rows from a query, format the rows into new arrays of coordinates with ID as the index.

Example response from query: This is an array of objects, returned from mysql query. Cleared up possible confusion.

[
    {
        id: 2031,
      store_id: 12,
      latitude: '40.734479',
      longitude: '-73.998943' 
    },
    {
        id: 2041,
        store_id: 12,
      latitude: '40.732912',
      longitude: '-74.000083' 
    },
    {
        id: 2051,
      store_id: 14,
      latitude: '40.731053',
      longitude: '-74.001423' 
    },
    {
        id: 2061,
        store_id: 14,
        latitude: '40.729696',
        longitude: '-74.002186' 
    },
    {
        id: 2071,
        store_id: 14,
        latitude: '40.729121',
        longitude: '-74.002473' 
    }
]

Desired output:

{
    12:[
        { latitude: '40.734479', longitude: '-73.998943' },
        { latitude: '40.732912', longitude: '-74.000083' }
    ],

    14:[
        { latitude: '40.731053', longitude: '-74.001423'},
        { latitude: '40.729696', longitude: '-74.002186'},
        { latitude: '40.729121', longitude: '-74.002473'}
    ]
}   

Previously, utilizing a for loop, I've checking if object had property, then creating property if not, then populated the arrays.

Is there a better way to achieve this with map and reduce and/or filter?

Thanks.

4
  • 1
    Strange input, looks like JSON but it's not... Commented Feb 13, 2018 at 1:25
  • Agreed. Can you clarify if those objects you listed are part of an array? Or are they contained in some other type of structure? Commented Feb 13, 2018 at 1:30
  • 1
    @GillesQuenot It's mysql query return. It's an array of objects. I just copied from console. -- I just cleared up the input to make this clear. Commented Feb 13, 2018 at 1:30
  • 1
    Possible duplicate of What is the most efficient method to groupby on a JavaScript array of objects? Commented Feb 13, 2018 at 2:11

1 Answer 1

3

You can achieve this with Array.reduce:

const myArr = [{
  id: 2031,
  store_id: 12,
  latitude: '40.734479',
  longitude: '-73.998943' 
}, {
  id: 2041,
  store_id: 12,
  latitude: '40.732912',
  longitude: '-74.000083' 
}, {
  id: 2051,
  store_id: 14,
  latitude: '40.731053',
  longitude: '-74.001423' 
}, {
  id: 2061,
  store_id: 14,
  latitude: '40.729696',
    longitude: '-74.002186' 
}, {
  id: 2071,
  store_id: 14,
  latitude: '40.729121',
  longitude: '-74.002473' 
}];

const merged = myArr.reduce((merged, item) => {
  if (!merged[item.store_id]) merged[item.store_id] = [];
  merged[item.store_id].push({
    latitude: item.latitude,
    longitude: item.longitude,
  });

  return merged;
}, {});

console.log(merged)

Sign up to request clarification or add additional context in comments.

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.