简体   繁体   中英

Get unique datetime string value from array of arrays in javascript

I want to loop over an array of arrays structure in order to filter out the duplicate datetime string values, and push the unique datetime string values sorted in a new array. I have tried to use a nested forEach() method in order to compare each current key value with all the key values inside the array arr , and push them inside the res array in case of non matching situation, but it seems not working properly.

Here the array of arrays structure

arr =[
   ["some_value", "2016-11-23 18:11", 1]
   ["some_value", "2016-11-23 18:01", 1]
   ["some_value", "2016-11-23 18:01", 2]
   ["some_value", "2016-11-23 18:01", 1]
   ["some_value", "2016-11-23 18:22", 1]
   ["some_value", "2016-11-23 18:23", 1]
   ["some_value", "2016-11-23 18:25", 1]
   ["some_value", "2016-11-23 18:24", 3]
   ["some_value", "2016-11-23 18:26", 1]
   ["some_value", "2016-11-23 18:27", 1]
];

What I want to obtain as result

res = ["2016-11-23 18:01",
       "2016-11-23 18:11", 
       "2016-11-23 18:22",
       "2016-11-23 18:23",
       "2016-11-23 18:24",
       "2016-11-23 18:25",
       "2016-11-23 18:26",
       "2016-11-23 18:27"];

Can someone give me some tips on order to understand how to proceed?

I would map the array of arrays to just an array of date strings:

arr = arrayOfArrays.map(x => x[1])

then if you convert it to set you get unique values

var set = new Set(arr)

if needed you can eventually turn it back to array

var uniq = Array.from(set)

for sorting

uniq.sort((a,b)=> new Date(a) - new Date(b))

 var arrayOfArrays = [ ["some_value", "2016-11-23 18:11", 1], ["some_value", "2016-11-23 18:01", 1], ["some_value", "2016-11-23 18:01", 2], ["some_value", "2016-11-23 18:01", 1], ["some_value", "2016-11-23 18:22", 1], ["some_value", "2016-11-23 18:23", 1], ["some_value", "2016-11-23 18:25", 1], ["some_value", "2016-11-23 18:24", 3], ["some_value", "2016-11-23 18:26", 1], ["some_value", "2016-11-23 18:27", 1], ]; var arr = arrayOfArrays.map(x => x[1]) var set = new Set(arr) var uniq = Array.from(set) uniq.sort((a,b)=> new Date(a) - new Date(b)) console.log(uniq) 

A solution using map and Set

You can map over the array to pluck the dates:

const dates = arr.map(item => item[1]);

then you can put them in a new Set which is an array that holds only unique items. calling .values on the created set will return its results.

 arr = [ ["some_value", "2016-11-23 18:11", 1], ["some_value", "2016-11-23 18:01", 1], ["some_value", "2016-11-23 18:01", 2], ["some_value", "2016-11-23 18:01", 1], ["some_value", "2016-11-23 18:22", 1], ["some_value", "2016-11-23 18:23", 1], ["some_value", "2016-11-23 18:25", 1], ["some_value", "2016-11-23 18:24", 3], ["some_value", "2016-11-23 18:26", 1], ["some_value", "2016-11-23 18:27", 1], ]; const dates = arr.map(item => item[1]); const result = new Set(dates); console.log('result: ', Array.from(result)) 

Learn more!

You can read more about sets here on MDN

Here is an image to better conceptualize the idea of the "functional style" map/filter/reduce array functions (filter is where you'd ommit the onions) :

在此处输入图片说明

Image Source: https://twitter.com/francesc/status/507942534388011008

I just want to answer with the helper of lodash . I just believe this is still relevant.

var arr =[
    ["some_value", "2016-11-23 18:11", 1],
    ["some_value", "2016-11-23 18:01", 1],
    ["some_value", "2016-11-23 18:01", 2],
    ["some_value", "2016-11-23 18:01", 1],
    ["some_value", "2016-11-23 18:22", 1],
    ["some_value", "2016-11-23 18:23", 1],
    ["some_value", "2016-11-23 18:25", 1],
    ["some_value", "2016-11-23 18:24", 3],
    ["some_value", "2016-11-23 18:26", 1],
    ["some_value", "2016-11-23 18:27", 1]
];

_.map(_.sortBy(_.uniqBy(arr, v => v[1]), v => v[1]), v => v[1]);
        var arr = arr = [
        ["some_value", "2016-11-23 18:11", 1],
        ["some_value", "2016-11-23 18:01", 1],
        ["some_value", "2016-11-23 18:01", 2],
        ["some_value", "2016-11-23 18:01", 1],
        ["some_value", "2016-11-23 18:22", 1],
        ["some_value", "2016-11-23 18:23", 1],
        ["some_value", "2016-11-23 18:25", 1],
        ["some_value", "2016-11-23 18:24", 3],
        ["some_value", "2016-11-23 18:26", 1],
        ["some_value", "2016-11-23 18:27", 1],
     ];
    var dates = arr.map(x => x[1])
    var uniqueArray = dates.filter(function (item, pos) {
        return dates.indexOf(item) == pos;
    });

You can try below code to get your desired output.

function onlyUnique(value, index, self) { 
  return self.indexOf(value) === index;
}

var arr =[
  ["some_value", "2016-11-23 18:11", 1],
  ["some_value", "2016-11-23 18:01", 1],
  ["some_value", "2016-11-23 18:01", 2],
  ["some_value", "2016-11-23 18:01", 1],
  ["some_value", "2016-11-23 18:22", 1],
  ["some_value", "2016-11-23 18:23", 1],
  ["some_value", "2016-11-23 18:25", 1],
  ["some_value", "2016-11-23 18:24", 3],
  ["some_value", "2016-11-23 18:26", 1],
  ["some_value", "2016-11-23 18:27", 1]
];

var rest = [];


arr.forEach(function(entry) {

rest.push(entry[1]) 
});

rest.sort();

var finalArr = rest.filter( onlyUnique );

console.log(finalArr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM