在javascript数组中排序和删除重复项(仅显示最新值)

时间:2019-06-26 14:07:22

标签: javascript arrays sorting duplicates

我有一个数组。

import geopandas as gpd
import geoplot as gplt

path = gpd.datasets.get_path('naturalearth_lowres')
gdf = gpd.read_file(path)

legend_labels = ['< 2.4', '2.4 - 6', '6 - 15', '15 - 38', '38 - 140 M']
gplt.choropleth(gdf, hue='pop_est', cmap='Blues', scheme='quantiles',
                legend=True, legend_labels=legend_labels)

我必须删除array1 = [ {playtime: 110, date: "05 Jun"}, {playtime: 115, date: "04 Jun"}, {playtime: 113, date: "05 Jun"}, {playtime: 120, date: "03 Jun"}, {playtime: 108, date: "02 Jun"}, {playtime: 114, date: "01 Jun"}, {playtime: 106, date: "30 May"}, {playtime: 90, date: "12 Jun"}, {playtime: 89, date: "12 Jun"}, {playtime: 89, date: "11 Jun"}, {playtime: 95, date: "11 Jun"}, {playtime: 99, date: "10 Jun"}, {playtime: 98, date: "06 Jun"}, {playtime: 96, date: "08 Jun"}, {playtime: 120, date: "06 Jun"}, {playtime: 102, date: "07 Jun"}, {playtime: 120, date: "09 Jun"}, {playtime: 190, date: "12 Jun"} ]; 的重复项(具有相同array1的数组被视为重复项),并且只需要最新的条目并按日期的降序对数组进行排序-最新日期应该在第一位。

我的第一阶段数组应该看起来像

date

然后,我必须拼接前5个条目,然后从旧到新显示日期(最新日期应该在最后);

最终数组应该看起来像

array1 = [
  {playtime: 90, date: "12 Jun"},
  {playtime: 89, date: "11 Jun"},
  {playtime: 99, date: "10 Jun"},
  {playtime: 120, date: "09 Jun"},
  {playtime: 96, date: "08 Jun"},
  {playtime: 102, date: "07 Jun"},
  {playtime: 98, date: "06 Jun"},
  {playtime: 110, date: "05 Jun"},
  {playtime: 115, date: "04 Jun"},
  {playtime: 120, date: "03 Jun"},
  {playtime: 108, date: "02 Jun"},
  {playtime: 114, date: "01 Jun"},
  {playtime: 106, date: "30 May"}
];

我尝试用

进行排序
array1 = [
  {playtime: 106, date: "30 May"},
  {playtime: 114, date: "01 Jun"},
  {playtime: 108, date: "02 Jun"},
  {playtime: 120, date: "03 Jun"},
  {playtime: 115, date: "04 Jun"}
];

但是,被卡住了。

5 个答案:

答案 0 :(得分:2)

您需要做的是首先从原始阵列中删除重复项。我喜欢reduce函数,因为在这种情况下,您需要遍历数组以查看数组其余部分中是否存在重复值。您检查数组是否尚未包含该值,然后将该值的首次出现推入返回的数组中。

然后通过排序,您需要某种方式将日期解析为可比较的值。 Date.parse会将字符串转换为纪元时间,然后可以对其进行比较。 (请记住,最好使用已建立的库,例如moment来在浏览器之间一致地处理日期。

array1.reduce((arr, obj)=>{
    if (arr.length && arr.some((o)=>o.date == obj.date)) return arr;
    arr.push(obj)
    return arr;
}, []).sort((a, b)=> {
    const d1 = Date.parse(a.date);
    const d2 = Date.parse(b.date);
    return d1 - d2;
}).splice(0,5)

答案 1 :(得分:0)

//src array
const array1 = [{playtime:110,date:'05 Jun'},{playtime:115,date:'04 Jun'},{playtime:113,date:'05 Jun'},{playtime:120,date:'03 Jun'},{playtime:108,date:'02 Jun'},{playtime:114,date:'01 Jun'},{playtime:106,date:'30 May'},{playtime:90,date:'12 Jun'},{playtime:89,date:'12 Jun'},{playtime:89,date:'11 Jun'},{playtime:95,date:'11 Jun'},{playtime:99,date:'10 Jun'},{playtime:98,date:'06 Jun'},{playtime:96,date:'08 Jun'},{playtime:120,date:'06 Jun'},{playtime:102,date:'07 Jun'},{playtime:120,date:'09 Jun'},{playtime:190,date:'12 Jun'}];

const res = array1
	//remove items with repetitive date value
	.reduce((res, item) => (res.every(resItem => resItem.date != item.date) ? res.push(item) : true, res), [])
	//sort by date in ascending order
	.sort((a, b) => new Date(a.date+(new Date()).getFullYear()) - new Date(b.date+(new Date()).getFullYear()))
	//cut off 5 items
	.slice(0, 5);

console.log(res);

答案 2 :(得分:0)

使用以下函数从阵列中删除重复项:

//Your array
array1=   [{playtime: 110, date: "05 Jun"},{playtime: 115, date: "04 Jun"},{playtime: 113, date: "05 Jun"},{playtime: 120, date: "03 Jun"},{playtime: 108, date: "02 Jun"},{playtime: 114, date: "01 Jun"},{playtime: 106, date: "30 May"},{playtime: 90, date: "12 Jun"},{playtime: 89, date: "12 Jun"},{playtime: 89, date: "11 Jun"},{playtime: 95, date: "11 Jun"},{playtime: 99, date: "10 Jun"},{playtime: 98, date: "06 Jun"},{playtime: 96, date: "08 Jun"},{playtime: 120, date: "06 Jun"},{playtime: 102, date: "07 Jun"},{playtime: 120, date: "09 Jun"},{playtime: 190, date: "12 Jun"}];

//Function to remove duplicate objects from array
  function getUnique(arr, comp) {

    const unique = arr
      .map(e => e[comp])

      // store the keys of the unique objects
      .map((e, i, final) => final.indexOf(e) === i && i)

      // eliminate the dead keys & store unique objects
      .filter(e => arr[e]).map(e => arr[e]);

    return unique;
  }

//Here you will get data without duplicates
newArray = getUnique(getUnique, 'date');

对数组进行排序的代码

newArray.sort((a, b) => {
    if (a['date'] < b['date']) return -1;
    if (a['date'] > b['date']) return 1;
    return 0;
 })

答案 3 :(得分:0)

我有点担心您的日期不包括年份,并且被当作字符串使用,但是我们可以在这些限制范围内工作。这可以通过对数组进行排序,排序,删除重复项,然后仅返回前5个元素来完成。

// Our original array
array1 =   [
  {playtime: 110, date: "05 Jun"},
  {playtime: 115, date: "04 Jun"},
  {playtime: 113, date: "05 Jun"},
  {playtime: 120, date: "03 Jun"},
  {playtime: 108, date: "02 Jun"},
  {playtime: 114, date: "01 Jun"},
  {playtime: 106, date: "30 May"},
  {playtime: 90, date: "12 Jun"},
  {playtime: 89, date: "12 Jun"},
  {playtime: 89, date: "11 Jun"},
  {playtime: 95, date: "11 Jun"},
  {playtime: 99, date: "10 Jun"},
  {playtime: 98, date: "06 Jun"},
  {playtime: 96, date: "08 Jun"},
  {playtime: 120, date: "06 Jun"},
  {playtime: 102, date: "07 Jun"},
  {playtime: 120, date: "09 Jun"},
  {playtime: 190, date: "12 Jun"}
];

// Sort by date ascending
array1 = array1.sort(
	(a,b) => new Date(a.date) - new Date(b.date)
);

// Remove duplicates based on sorted date
var uniqueArray = [];
for(var i=0; i<array1.length; i++){
    // If we're on the first element, add it
    if(i === 0) uniqueArray.push(array1[i]);
  
    // For every subsequent element, if it's different than the last one, add it
    else if(array1[i].date !== array1[i-1].date){
  	uniqueArray.push(array1[i]);
  }
}

// Then slice first 5 elements
uniqueArray = uniqueArray.slice(0, 5);

// The results match what you need
console.log(uniqueArray);

答案 4 :(得分:0)

Ramda.js使此操作非常容易!

const input = [
  {playtime: 110, date: "05 Jun"},
  {playtime: 115, date: "04 Jun"},
  {playtime: 113, date: "05 Jun"},
  {playtime: 120, date: "03 Jun"},
  {playtime: 108, date: "02 Jun"},
  {playtime: 114, date: "01 Jun"},
  {playtime: 106, date: "30 May"},
  {playtime: 90, date: "12 Jun"},
  {playtime: 89, date: "12 Jun"},
  {playtime: 89, date: "11 Jun"},
  {playtime: 95, date: "11 Jun"},
  {playtime: 99, date: "10 Jun"},
  {playtime: 98, date: "06 Jun"},
  {playtime: 96, date: "08 Jun"},
  {playtime: 120, date: "06 Jun"},
  {playtime: 102, date: "07 Jun"},
  {playtime: 120, date: "09 Jun"},
  {playtime: 190, date: "12 Jun"}
];

const f = R.pipe(
  R.uniqBy(R.prop("date")),
  R.sortBy(R.pipe(R.prop("date"), Date.parse)),
  R.take(5),
)

console.log(f(input))
<script src="//cdn.jsdelivr.net/npm/ramda@latest/dist/ramda.min.js"></script>