JavaScript Get unique datetime string value from array of arrays in javascript unique datetime string c#

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?

Answer:1

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

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

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]);
Answer:4
        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;
    });
Answer:5

last time I found this piece of code included in some tutorial. It just prints the numbers in the defined range. <!DOCTYPE html> <html lang="pl"> <html> <head> ...

last time I found this piece of code included in some tutorial. It just prints the numbers in the defined range. <!DOCTYPE html> <html lang="pl"> <html> <head> ...

  1. javascript write function with callback
  2. javascript write function return value
  3. javascript write function
  4. javascript write function as string
  5. javascript write function with parameter
  6. javascript write function example
  7. write javascript function in html
  8. write javascript function in onclick
  9. write javascript function in typescript
  10. write javascript function in console
  11. write javascript function in html tag
  12. write javascript function in php
  13. write javascript function in ts file
  14. write javascript function in react
  15. write javascript function inside onclick
  16. write javascript function in js file
  17. javascript function write text
  18. javascript function write to file
  19. write javascript function in code behind c#
  20. write javascript function inside html

I have an array of objects which I wish to convert into a plain Immutable map of: name: { urlname id } Currently I am doing the following data being the array of objects: data = data.map((data) ...

I have an array of objects which I wish to convert into a plain Immutable map of: name: { urlname id } Currently I am doing the following data being the array of objects: data = data.map((data) ...

  1. convert array objects to json
  2. convert array objects to string
  3. convert array objects to string javascript
  4. convert array objects javascript
  5. converting array to object php
  6. convert array of objects to list c#
  7. convert array of objects to json javascript
  8. convert array of objects to map javascript
  9. convert array of objects to json c#
  10. convert array of objects to csv javascript
  11. convert array to objects
  12. convert array of objects to csv
  13. convert array of objects to list java
  14. convert array of objects to key value pair
  15. convert array of objects to json php
  16. convert array of objects to json python
  17. convert array of objects to string javascript
  18. convert array of objects to dictionary swift
  19. convert array of objects into json
  20. convert array of objects to json swift

I want to disable and replace the vertical scroll with horizontal one in Vanilla JavaScript. I am blocked when I try to replace my scroll values. I trust there is a simple simple (really simple) ...

I want to disable and replace the vertical scroll with horizontal one in Vanilla JavaScript. I am blocked when I try to replace my scroll values. I trust there is a simple simple (really simple) ...

I'm using https://www.npmjs.com/package/mqtt with React. In my component I have: componentDidMount:function(){ client.subscribe('test/topic'); client.on('message',function(topic,...

I'm using https://www.npmjs.com/package/mqtt with React. In my component I have: componentDidMount:function(){ client.subscribe('test/topic'); client.on('message',function(topic,...

  1. react mqtt subscribe