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

javascript - How to transform and filter an array - Stack Overflow

programmeradmin4浏览0评论

I have an array with objects like this:

[
  {
    latitude: 00,
    longitude: 11
  },
  {
    latitude: 22,
    longitude: 33
  },
  {
    latitude: 00,
    longitude: 11
  }, 
]

I would like to convert and filter that array to another array, which contains arrays, which contain objects:

if longitude and latitude are equals, they should go to the same array, so, I need a new array like this:

[
  [
    {
      latitude: 00,
      longitude: 11
    },
    {
      latitude: 00,
      longitude: 11
    }
  ],
  [
    {
      latitude: 22,
      longitude: 33
    }
  ]
]

I have an array with objects like this:

[
  {
    latitude: 00,
    longitude: 11
  },
  {
    latitude: 22,
    longitude: 33
  },
  {
    latitude: 00,
    longitude: 11
  }, 
]

I would like to convert and filter that array to another array, which contains arrays, which contain objects:

if longitude and latitude are equals, they should go to the same array, so, I need a new array like this:

[
  [
    {
      latitude: 00,
      longitude: 11
    },
    {
      latitude: 00,
      longitude: 11
    }
  ],
  [
    {
      latitude: 22,
      longitude: 33
    }
  ]
]
Share Improve this question edited May 21, 2018 at 9:08 Mihai Alexandru-Ionut 48.5k14 gold badges105 silver badges132 bronze badges asked May 21, 2018 at 8:51 Alexander RodinAlexander Rodin 811 silver badge7 bronze badges 2
  • 1 Where’s your attempt at this? See How much research effort is expected of Stack Overflow users? – Sebastian Simon Commented May 21, 2018 at 8:57
  • What is your expected output for input, [{longitude:11,latitude:00},{latitude:22,longitude:33},{latitude:00,longitude:11},] ? Note that JSON.stringify will create a different string if the same object properties are ordered differently. This means your current accepted answer will group objects with the same latitude and longitude in different arrays if the latitude and longitude properties are ordered differently. – גלעד ברקן Commented May 21, 2018 at 12:44
Add a ment  | 

6 Answers 6

Reset to default 4

reduce into an object whose keys represent the array objects' contents, and whose values are arrays, and create/push to the appropriate array:

const input=[{latitude:00,longitude:11},{latitude:22,longitude:33},{latitude:00,longitude:11},]
const output = Object.values(
  input.reduce((a, obj) => {
    const key = JSON.stringify(obj);
    if (!a[key]) a[key] = [];
    a[key].push(obj);
    return a;
  }, {})
)
console.log(output);

I would suggest you to add count field in JSON object add update count if you found same element twice otherwise above answer are looking good.

var data = [{
        latitude: 00,
        longitude: 11
    },
    {
        latitude: 22,
        longitude: 33
    },
    {
        latitude: 00,
        longitude: 11
    },
]
var result = [];
data.forEach(val => {
    const filterIndex = result.findIndex(v => v.latitude == val.latitude && v.longitude == val.longitude);
    filterIndex == -1 ? result.push({ ...val,
        count: 1
    }) : result[filterIndex].count += 1;
});
console.log(result);

You can use latitude and longitude to create a unique key and push each object in an array for each unique key in an object accumulator. Then using Object.values() extract all values.

const data = [ { latitude: 00, longitude: 11 }, { latitude: 22, longitude: 33 }, { latitude: 00, longitude: 11 }, ],
    result = Object.values(data.reduce((r,{latitude, longitude}) => {
      const key = latitude + '_' + longitude;
      r[key] = r[key] || [];
      r[key].push({latitude, longitude});
      return r;
    },{}));
console.log(result);

You can reduce your array into an object with latitude and longitude forms a unique key and push the values corresponding to it. And then get the array back using Object.values

Try following

var arr = [ { latitude: 00, longitude: 11 }, { latitude: 22, longitude: 33 }, { latitude: 00, longitude: 11 }];

var map = arr.reduce((a,c) => {
var key = c.latitude + "_" + c.longitude;
  a[key] = a[key] || [];
  a[key].push(c);
  return a;
}, {});

console.log(Object.values(map));

For reference, Array.reduce and Object.values

You could use a posed key for a hashtable to speed up the duplicate finding:

 const result = [], hash = {};

 for(const {latitude, longitude} of array) {
   const key = latitude + "_" + longitude;
   if(hash[key]) {
     hash[key].push({longitude, latitude});
   } else {
     result.push(hash[key] = [{longitude, latitude}]);
   }
 }

This si the solution for the filtering first you reduce data in groups, then you concat to a string.

    var a = [{latitude:21,longitude:12},{latitude:211,longitude:2},];

    var mapped = a.reduce((acc, e) => {
        var key = '' +  e.latitude + ','+ e.longitude;
        if (!acc[key])  acc[key] = [e];
        else acc[key].push(e);	
       return acc;
    }, {});

    console.log(Object.keys(mapped).map((key) => mapped[key]));

    // edit
    console.log(Object.values(mapped));
    

发布评论

评论列表(0)

  1. 暂无评论