使用 linqjs 使用来自另一个数组的值过滤一个数组

Filter one array using values from another array using linqjs

第一个是对象数组:

let objectArray = [{
    FullName: "Person1",
    PersonId: "id1"
  },
  {
    FullName: "Person2",
    PersonId: "id2"
  },
  {
    FullName: "Person3",
    PersonId: "id3"
  },
  {
    FullName: "Person4",
    PersonId: "id4"
  }
];

第二个是包含一些 id 的字符串数组。

let idsArray= ["id1", "id2", "id3"];

我需要删除第一个数组的对象,其id包含在第二个数组中。

预期结果:

firstArray = [{
  FullName: "Person4",
  PersonId: "id4"
}];

探索 Linqjs 文档我发现 Except() 方法允许我使用第二个数组作为 "filter".

从第一个数组中删除元素

为了使用此方法,我需要从 objectArray 创建一个新数组,该数组仅包含其 ID 包含在 idsArray 中的元素,以将其用作参数。

示例:

let filteredArray = Enumerable.From(objectArray).Except(theNewArray).ToArray();

要创建这个新数组,我可以使用 Linqjs 中的方法 Where()

我的问题从这里开始,因为考虑到我有一个要过滤的 ID 数组,我不知道如何创建这个新数组。

您可以使用filter() to filter the array. Use new Set()创建集合对象。这将使检查 PersonId 是否存在变得更容易。无需循环每个 filter()

let objectArray = [
  {FullName: "Person1",PersonId: "id1"},
  {FullName: "Person2",PersonId: "id2"},
  {FullName: "Person3",PersonId: "id3"},
  {FullName: "Person4",PersonId: "id4"}
];

let idsArray = ["id1", "id2", "id3"];

let idsToRemove = new Set(idsArray);
let result = objectArray.filter(o => !idsToRemove.has(o.PersonId));

console.log(result);

另一个选项是使用 includes() 来测试数组是否包含某个字符串。

let objectArray = [
  {FullName: "Person1",PersonId: "id1"},
  {FullName: "Person2",PersonId: "id2"},
  {FullName: "Person3",PersonId: "id3"},
  {FullName: "Person4",PersonId: "id4"}
];

let idsArray = ["id1", "id2", "id3"];

let result = objectArray.filter(o => !idsArray.includes(o.PersonId));

console.log(result);


注意:如果你不想要一个新的变量,你可以覆盖现有的变量:

objectArray = objectArray.filter(o => ...);

您可以像这样使用 Vanilla JavaScript 的 array.filter and array.includes

let objectArray = [
  {FullName: "Person1", PersonId: "id1"},
  {FullName: "Person2", PersonId: "id2"},
  {FullName: "Person3", PersonId: "id3"},
  {FullName: "Person4", PersonId: "id4"}
];
    
  let excludeIdsArray= ["id1", "id2", "id3"];
  
  let newObj = objectArray.filter(obj => !excludeIdsArray.includes(obj.PersonId))
  
  console.log(newObj)

,你可以这样使用array.reduce and array.includes

let objectArray = [
  {FullName: "Person1", PersonId: "id1"},
  {FullName: "Person2", PersonId: "id2"},
  {FullName: "Person3", PersonId: "id3"},
  {FullName: "Person4", PersonId: "id4"}
];
        
let excludeIdsArray= ["id1", "id2", "id3"];

let newObj = objectArray.reduce((arr, myObject) => {
  if(!excludeIdsArray.includes(myObject.PersonId)) {
    arr.push(myObject)
  } 
  return arr
}, [])
  

console.log(newObj)

您可以结合使用 Array.prototype.filter 方法和 indexOf 来测试是否在要排除的 ID 数组中找到 PersonId 属性 - 如果没有,将其添加到新的 filteredArray。见下面的例子:

let objects = [{
    FullName: "Person1",
    PersonId: "id1"
  },
  {
    FullName: "Person2",
    PersonId: "id2"
  },
  {
    FullName: "Person3",
    PersonId: "id3"
  },
  {
    FullName: "Person4",
    PersonId: "id4"
  }
];

let toDelete = ["id1", "id2", "id3"];

//just use Array.prototype.filter method to remove unwanted
var filteredObjects = objects.filter(function(element) {
  return toDelete.indexOf(element.PersonId) === -1;
});

console.log(filteredObjects);

这是使用原版 JavaScript 实现的。如果这是您唯一使用它的目的,我建议您从项目的代码库中删除 linqjs。

您可以使用对象数组和列参数来获取 linq.js 中的 Except,以排除不需要的 PersonId

var objectArray = [{ FullName: "Person1", PersonId: "id1" }, { FullName: "Person2", PersonId: "id2" }, { FullName: "Person3", PersonId: "id3" }, { FullName: "Person4", PersonId: "id4" }],
    idsArray = ["id1", "id2", "id3"],
    result = Enumerable
        .From(objectArray)
        .Except(
            Enumerable.From(idsArray).Select("{ PersonId: $ }"),
            "$.PersonId"
        )
        .ToArray();

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>