从javascript数组中获取唯一的datetime字符串值。

时间:2022-06-23 12:20:49

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.

我希望遍历数组结构的数组,以便过滤出重复的datetime字符串值,并将唯一的datetime字符串值按新数组进行排序。我尝试使用嵌套forEach()方法,以便将每个当前键值与数组arr中的所有键值进行比较,并将它们推入res数组中,以防出现不匹配的情况,但它似乎不能正常工作。

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 个解决方案

#1


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)

#2


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.

然后你可以把它们放到一个新的集合中,这个集合是一个只包含唯一项的数组。在创建的集合上调用.values将返回结果。

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

您可以在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):

这里是一个图像,以便更好地概念化“功能风格”的地图/过滤器/减少数组功能(过滤器是你在哪里ommit洋葱):

从javascript数组中获取唯一的datetime字符串值。

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

图片来源:https://twitter.com/francesc/status/507942534388011008

#3


1  

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

我只是想用lodash的助手来回答。我只是相信这仍然是相关的。

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

#4


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

#5


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

#1


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)

#2


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.

然后你可以把它们放到一个新的集合中,这个集合是一个只包含唯一项的数组。在创建的集合上调用.values将返回结果。

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

您可以在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):

这里是一个图像,以便更好地概念化“功能风格”的地图/过滤器/减少数组功能(过滤器是你在哪里ommit洋葱):

从javascript数组中获取唯一的datetime字符串值。

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

图片来源:https://twitter.com/francesc/status/507942534388011008

#3


1  

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

我只是想用lodash的助手来回答。我只是相信这仍然是相关的。

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

#4


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

#5


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