2

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?

5 Answers 5

3

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)

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

3 Comments

In this specific circumstance what a and b stand for in the .sort() method? Since in the moz documentation it refers to 2 parameter values to compare with. I have tried to console.log the result of datetime differences and I didn't exactly get the point.
a and b represent two elements of the array, if the function returns something below 0 a is ordered before b, otherwise after
glad I could help
3

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):

enter image description here

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

Comments

1

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]);

1 Comment

Interesting library.. I'll take a look! Thanks
0
        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;
    });

Comments

0

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);

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.