第一个是对象数组:
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()
方法允许我使用第二个数组作为“过滤器”从第一个数组中删除元素。
要使用此方法,我需要从objectArray
创建一个新数组,该数组仅包含其ID包含在idsArray
上的元素以用作参数。
示例:
let filteredArray = Enumerable.From(objectArray).Except(theNewArray).ToArray();
要创建此新数组,我可以使用Where()
中的方法Linqjs
。
我的问题从这里开始,因为考虑到我要过滤的ID数组,我不知道如何创建这个新数组。
答案 0 :(得分:4)
您可以使用filter()
来过滤数组。使用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 => ...);
答案 1 :(得分:4)
您可以像下面这样使用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)
或者,您可以像这样使用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)
答案 2 :(得分:3)
您可以结合使用string commandName = e.CommandName.ToString().Trim();
GridViewRow row = gvRestaurant.Rows[Convert.ToInt32(e.CommandArgument)];
switch (commandName)
{
case "ViewMore":
List<Restaurant> restaurants = RestaurantDB.getRestaurantByCountry(Session["countryName"].ToString());
Restaurant r = restaurants[gvRestaurant.SelectedIndex];
Session["menuItems"] = r;
Session["menu"] = r.Name;
Response.Redirect("GetMenuItem.aspx");
break;
case "MakeReservation":
List<Restaurant> rs = RestaurantDB.getRestaurantByUserSelected(Session["countryName"].ToString(), Session["menu"].ToString());
Restaurant r1 = rs[gvRestaurant.SelectedIndex];
Session["menu"] = r1.Name;
Response.Redirect("MakeReservation.aspx");
break;
default: break;
方法和Array.prototype.filter
来测试是否在要排除的ID数组中找到indexOf
属性-如果找不到,请将其添加到新的{ {1}}。参见以下示例:
PersonId
这是使用原始JavaScript实现的。如果这是您唯一要使用的,我建议您从项目的代码库中删除linqjs。
答案 3 :(得分:1)
您可以将Except
中的linq.js
与对象数组和列参数一起使用,以排除不需要的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>