首页 > javascript > 从一系列词典中获取独特的词典?

从一系列词典中获取独特的词典? (Get unique dictionaries from array of dictionaries?)

问题

我有一个看起来像这样的数组:

dateArray = [
      {year:'2018', month: "01"},
      {year:'2018', month: "01"},
      {year:'2017', month: "02"},
      {year:'2017', month: "02"}
    ];

我想从该数组中获取唯一值并尝试这样做:

  let unique = [...new Set(this.state.dateArray.map(item => item.Group))];
  console.log('UNIQUE: ',unique);

此解决方案不起作用。如何在字典数组中获取唯一字典并将它们放入新数组中?

解决方法

可能有一种更干净的方式,但这会对你的数组进行重复数据删除:

const dateArray = [
  {year:'2018', month: "01"},
  {year:'2018', month: "01"},
  {year:'2017', month: "02"},
  {year:'2017', month: "02"}
];

const uniqueDates = [];
for (let date of dateArray){ // Use "of" to loop through an array
  let unique = true; // Set a flag to handle possible unwanted items 
  for (let uniqueDate of uniqueDates){
    if (uniqueDate.year == date.year && uniqueDate.month == date.month){
      unique = false; // Both properties match, so throw up the flag
    }
  }
  if(unique){ // Only add the item if the flag didn't go up
    uniqueDates.push(date);
  }
}
console.log(uniqueDates);



或者同样的事情,但使用.forEach(它将回调函数作为参数,可以是ES6箭头函数。)

const uniqueDates = [];
dateArray.forEach(date => { // the argument passed to the callback stores the current item
  let unique = true;
  uniqueDates.forEach(uniqueDate => {
    if (uniqueDate.year == date.year && uniqueDate.month == date.month){
      unique = false;    
    }
  });
  if(unique){
    uniqueDates.push(date);
  }
});
console.log(uniqueDates);

问题

I have an array that looks like this:

dateArray = [
      {year:'2018', month: "01"},
      {year:'2018', month: "01"},
      {year:'2017', month: "02"},
      {year:'2017', month: "02"}
    ];

I'd like to get the unique values from that array and have tried doing it like this:

  let unique = [...new Set(this.state.dateArray.map(item => item.Group))];
  console.log('UNIQUE: ',unique);

This solution does not work. How can I get the unique dictionaries in an array of dictionaries and put them in a new array?

解决方法

There's probably a cleaner way, but this deduplicates your array:

const dateArray = [
  {year:'2018', month: "01"},
  {year:'2018', month: "01"},
  {year:'2017', month: "02"},
  {year:'2017', month: "02"}
];

const uniqueDates = [];
for (let date of dateArray){ // Use "of" to loop through an array
  let unique = true; // Set a flag to handle possible unwanted items 
  for (let uniqueDate of uniqueDates){
    if (uniqueDate.year == date.year && uniqueDate.month == date.month){
      unique = false; // Both properties match, so throw up the flag
    }
  }
  if(unique){ // Only add the item if the flag didn't go up
    uniqueDates.push(date);
  }
}
console.log(uniqueDates);

Or the same thing, but using .forEach (which takes a callback function as an argument, which can be an ES6 arrow function.)

const uniqueDates = [];
dateArray.forEach(date => { // the argument passed to the callback stores the current item
  let unique = true;
  uniqueDates.forEach(uniqueDate => {
    if (uniqueDate.year == date.year && uniqueDate.month == date.month){
      unique = false;    
    }
  });
  if(unique){
    uniqueDates.push(date);
  }
});
console.log(uniqueDates);
相似信息