[英]Filter one array using values from another array using linqjs
The first one is an array of objects: 第一个是对象数组:
let objectArray = [{
FullName: "Person1",
PersonId: "id1"
},
{
FullName: "Person2",
PersonId: "id2"
},
{
FullName: "Person3",
PersonId: "id3"
},
{
FullName: "Person4",
PersonId: "id4"
}
];
The second one is an array of strings containing some ids. 第二个是包含一些ID的字符串数组。
let idsArray= ["id1", "id2", "id3"];
I need to delete the objects of the first array whose id are contained in the second array. 我需要删除ID包含在第二个数组中的第一个数组的对象。
Expected result: 预期结果:
firstArray = [{
FullName: "Person4",
PersonId: "id4"
}];
Exploring Linqjs
documentation I discovered that the Except()
method allows me to remove elements from the first array using the second one as the "filter". 浏览Linqjs
文档时,我发现Except()
方法允许我使用第二个数组作为“过滤器”从第一个数组中删除元素。
In order to use this method, I need to create a new array from objectArray
that contains only the elements whose ids are contained on idsArray
to use it as a parameter. 为了使用这种方法,我需要创建一个新的阵列objectArray
只包含其ID包含在元素idsArray
使用它作为一个参数。
Example: 例:
let filteredArray = Enumerable.From(objectArray).Except(theNewArray).ToArray();
To create this new array I can use the method Where()
from Linqjs
. 要创建这个新数组,我可以使用Linqjs
Where()
方法。
My problem starts here because I don't know how to create this new array considering that I have an array of ids to filter. 我的问题从这里开始,因为考虑到我要过滤的ID数组,我不知道如何创建这个新数组。
You can use filter()
to filter the array. 您可以使用filter()
过滤数组。 Use new Set()
to create a set object. 使用new Set()
创建一个set对象。 This will make easier to check if the PersonId
exists. 这将使检查PersonId
存在更加容易。 No need to loop every filter()
无需循环每个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);
Another option is using includes()
to test if an array includes a certain string. 另一种选择是使用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);
Note: If you dont want a new variable, you can override the existing variable as: 注意:如果您不想使用新变量,则可以通过以下方式覆盖现有变量:
objectArray = objectArray.filter(o => ...);
You can use Vanilla JavaScript's array.filter
and array.includes
like this: 您可以使用Vanilla JavaScript的array.filter
和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)
Or , you can use array.reduce
and array.includes
like this: 或者 ,你可以使用array.reduce
和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)
You can use Array.prototype.filter
method in conjunction with indexOf
to test if the PersonId
property is found in the array of IDs to exclude - if it's not, add it to the new filteredArray
. 您可以将Array.prototype.filter
方法与indexOf
结合使用,以测试是否在要排除的ID数组中找到PersonId
属性-如果找不到,请将其添加到新的filteredArray
。 See below for example: 参见以下示例:
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);
This is achieved using vanilla JavaScript. 这是使用香草JavaScript实现的。 I would advise you remove linqjs from your project's codebase if this is the only thing you're using it for. 如果这是您唯一要使用的,我建议您从项目的代码库中删除linqjs。
You could take Except
of linq.js
with an array of objects and a column parameter to exclude the unwanted PersonId
. 您可以使用带有对象数组和列参数的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.