开发者问题收集

如果一个值相同,则过滤掉数组中的对象

2020-02-18
1747

我正在从一个 API 中获取数据,该 API 有时会给我多个具有相同值或非常相似值的对象,我想删除这些对象。

例如,我可能会返回:

  [
   {
    "Name": "blah",
    "Date": "1992-02-18T00:00:00.000Z",
    "Language": "English",
  },
   {
    "Name": "blahzay",
    "Date": "1998-02-18T00:00:00.000Z",
    "Language": "French",
  },       {
    "Name": "blah",                     // same name, no problem
    "Date": "1999-02-18T00:00:00.000Z", // different date
    "Language": "English",             // but same language
  },
 ]

因此,我想检查没有两个对象具有具有相同“语言”值(在本例中为“英语”)的键。

如果“语言”值重复,我希望获得过滤掉整个对象的一般过程,但每次返回的对象数量不相同。因此,允许数组中的对象数量动态变化。

这里有一个例子: 将一个对象数组与另外两个对象数组进行过滤时出现意外结果

但前提是数组中有一定数量的对象,并且每次只比较相同对象的内容。

我会寻找一种方法来比较

  arrayName[eachObject].Language === "English"

并保留其中一个对象,但任何其他对象(未知数量的对象)都应该被过滤掉,最有可能使用 .filter() 方法和 .map()。

3个回答

以下代码片段将遇到的语言存储在数组中。如果当前对象的语言在数组中,则会将其过滤掉。它假设存储了遇到的第一个与该语言相关的对象。

const objs = [
   {
    "Name": "blah",
    "Date": "1992-02-18T00:00:00.000Z",
    "Language": "English",
  },
   {
    "Name": "blahzay",
    "Date": "1998-02-18T00:00:00.000Z",
    "Language": "French",
  },       {
    "Name": "blah",                     // same name, no problem
    "Date": "1999-02-18T00:00:00.000Z", // different date
    "Language": "English",             // but same language
  },
 ],
 presentLanguages = [];
 let languageIsNotPresent;
 const objsFilteredByLanguage = objs.filter(function (o) {
  languageIsNotPresent = presentLanguages.indexOf(o.Language) == -1;
  presentLanguages.push(o.Language);
  return languageIsNotPresent;
 });
 console.log(objsFilteredByLanguage);
tstrand66
2020-02-18

您可以获取哈希表并通过检查 NameLanguage 来过滤数组。

var array = [{ Name: "blah", Date: "1992-02-18T00:00:00.000Z", Language: "English" }, { Name: "blahzay", Date: "1998-02-18T00:00:00.000Z", Language: "French" }, { Name: "blah", Date: "1999-02-18T00:00:00.000Z", Language: "English" }],
    hash = {},
    result = array.filter(({ Name, Language }) => {
        var key = `${Name}|${Language}`;
        if (!hash[key]) return hash[key] = true;
    });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
2020-02-18

使用 Set 可以轻松删除任意数量的重复项。我尽量详细,以便每个步骤都清晰明了。

var objects = [{ "Name": "blah", "Date": "1992-02-18T00:00:00.000Z", "Language": "English", }, { "Name": "blah", "Date": "1998-02-18T00:00:00.000Z", "Language": "French", }, { "Name": "blah", "Date": "1999-02-18T00:00:00.000Z", "Language": "English" }];

function uniqueKeyVals(objects, key) {
  const objVals = objects.map(object => object[key]); // ex. ["English", "French", "English"]  
  return objects.slice(0, new Set(objVals).size);     // ex. { "English", "French" }.size = 2
}

function removeKeyDuplicates(objects, keys) {
  keys.forEach(key => objects = uniqueKeyVals(objects, key));
  return objects;
}

// can also use uniqueKeyVals(key) directly for just one key
console.log("Unique 'Language': \n", removeKeyDuplicates(objects, ["Language"]));
console.log("Unique ['Language', 'Name']: \n", removeKeyDuplicates(objects, ["Language", "Name"]));
Lewis
2020-02-18