卸下基于按键及放数组对象;在另一个对象的属性? [英] Remove objects from array based on keys & properties in another object?
问题描述
我有以下对象,其属性项设置为用户名和属性值被设置为它们出现的次数的计数,和包含对象的具有属性的列表的阵列用户ID
和用户名
:
peopleWhoAppear = {爱德华:5,山猫:2,迈克尔:9,安迪:6}siteUsers = [
{用户名:1842年,用户名:萨姆},
{用户名:2943,用户名:夏洛特},
{用户名:1842年,用户名:理查德},
{用户名:2916,用户名:伊万},
{用户名:2073,用户名:保罗},
{用户名:1842年,用户名:西奥}
...
]
我想从 siteUsers
数组,其中用户名
在属性键匹配删除所有对象的 peopleWhoAppear
对象如果属性值一定数量的匹配(也可能是5或8),其余结果返回给另一个数组。我一直停留在这6小时超过2天了,我还没有取得任何进展。
我试过不同的排列 .filter
和 .splice
,但没有成功的循环。例如:
为(以peopleWhoAppear VAR用户名){
如果(peopleWhoAppear [用户名] ===计数){
siteUsers.filter(功能(人){
的console.log(人);
返回person.UserName ==验证!;
}));
}
}
我怎样才能做到这一点?我没有访问除了jQuery的任何图书馆所以像下划线是没有用在这里。
我试过变化
.filter
的排列和循环和.splice
,但没有成功。
块引用>您只需要
过滤器
:返回siteUsers.filter(函数(OBJ){
VAR用户= obj.UserName;
(;&安培;在peopleWhoAppear和放大器用户peopleWhoAppear [用户] ==计数)回来吧!;
});这将让你一个新的数组,与所有没有出现在
peoplWhoAppear
或在那里有错误计数的用户。I have the following object, where the property key is set to a username and the property value is set to a count of the number of times they appear, and an array containing a list of objects with properties of
UserId
, andUserName
:peopleWhoAppear = { "Edward": 5, "Charlotte": 2, "Michael": 9, "Andy": 6 } siteUsers = [ { UserId: 1842, UserName: "Sam" }, { UserId: 2943, UserName: "Charlotte" }, { UserId: 1842, UserName: "Richard" }, { UserId: 2916, UserName: "Ivan" }, { UserId: 2073, UserName: "Paul" }, { UserId: 1842, UserName: "Theo" } ... ]
I would like to remove all objects from the
siteUsers
array where theUserName
matches a property key in thepeopleWhoAppear
object if the property value matches a certain number (it could be 5 or 8) and return the remaining results to another array. I've been stuck on this for 6 hours over 2 days now and I've not made any progress.I've tried varies permutations and loops of
.filter
and.splice
, but with no success. For example:for (var UserName in peopleWhoAppear) { if (peopleWhoAppear[UserName ] === count) { siteUsers.filter(function (person) { console.log(person); return person.UserName !== verifier; })); } }
How can I accomplish this? I don't have access to any libraries apart from jQuery so things like Underscore are not useful here.
解决方案I've tried varies permutations and loops of
.filter
and.splice
, but with no success.You will only need
filter
:return siteUsers.filter(function(obj) { var user = obj.UserName; return !(user in peopleWhoAppear && peopleWhoAppear[user] == count); });
This will get you a new array, with all the users that did not appear in
peoplWhoAppear
or had the wrong count in there.这篇关于卸下基于按键及放数组对象;在另一个对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!