首页 \ 问答 \ 是否有`orderBy`过滤器功能,它允许它正确处理可选属性?(Is there `orderBy` filter feature that allows it to handle optional properties correctly?)

是否有`orderBy`过滤器功能,它允许它正确处理可选属性?(Is there `orderBy` filter feature that allows it to handle optional properties correctly?)

是否有一种简单,优雅的方式来按顺序排序数组,但将任何缺失值视为0 (或任意设置值)而不是undefined

例如,以下代码

<li ng-repeat="item in items | orderBy:['-optionalKey', 'secondaryKey']">{{ item.secondaryKey }}</li>

用数据集

$scope.items = [
    {secondaryKey: 75, optionalKey: 25},
    {secondaryKey: 100},
    {secondaryKey: 50, optionalKey: 0},
];

会产生这个

<li>75</li>
<li>50</li>
<li>100</li>

而不是这个

<li>100</li>
<li>75</li>
<li>50</li>

在这里玩它: http//plnkr.co/edit/G3nQyEzHfUiiovJTD44Y?p = preview

我意识到我可以重新实现整个orderBy过滤器,但这不是很orderBy维护或面向未来。 我还可以在常规orderBy过滤器中使用函数作为谓词,但是我仍然需要重新实现已经内置到orderBy的复杂参数处理(例如深度键嵌套, + / -反转等)。 我真正想知道的是,如果有一些我不知道的语法可以让我做这样的事情:

<li ng-repeat="item in items | orderBy:['-optionalKey', 'secondaryKey']:false:0">{{ item.secondaryKey }}</li>

...其中第三个参数( 0 )表示我想要将falsy / missing键视为的值。


Is there a simple, elegant way to order an array as per usual but treat any missing values as 0 (or any arbitrarily set value) instead of undefined?

e.g. so that the following code

<li ng-repeat="item in items | orderBy:['-optionalKey', 'secondaryKey']">{{ item.secondaryKey }}</li>

with a dataset like

$scope.items = [
    {secondaryKey: 75, optionalKey: 25},
    {secondaryKey: 100},
    {secondaryKey: 50, optionalKey: 0},
];

would produce this

<li>75</li>
<li>50</li>
<li>100</li>

instead of this

<li>100</li>
<li>75</li>
<li>50</li>

?

Play with it here: http://plnkr.co/edit/G3nQyEzHfUiiovJTD44Y?p=preview

I realize I could reimplement the entire orderBy filter, but that's not very maintainable or future-proof. I could also use a function as a predicate inside the regular orderBy filter, but then I would still have to reimplement the complex arguments handling that's already built-in to orderBy (e.g. deep key nesting, +/- reversing, etc.). What I'm really wondering is if there's some syntax I'm not aware of that would allow me to do something like this:

<li ng-repeat="item in items | orderBy:['-optionalKey', 'secondaryKey']:false:0">{{ item.secondaryKey }}</li>

…where the third argument (0) represents the value I want to treat falsy/missing keys as.


原文:https://stackoverflow.com/questions/27117227
更新时间:2023-12-07 19:12

最满意答案

当我们说SQL Server然后我们说RDBMS。

然后有规范化 INF,2NF和3rdNF ......,这是你如何规范用户和他的朋友数据库的问题。

我的观点,你需要三张桌子

Individual (IndId (PK), Name ...)
User (UserId (PK), IndId (FK to Individual Table), login, password etc)
Friends(FID(PK), UserId (FK to User Table), IndId (FK to Individual Table))

获取用户的所有朋友

Select I.* from Friends F
     JOIN User U on F.UserId=U.UserId // Get all friends of all users
     JOIN Individual I on I.IndId =F.IndId // Get there names etc
   Where I.Name = 'MyFriend' // filter a friend

When we say SQL Server then we are saying RDBMS.

Then there is normalization INF, 2NFand 3rdNF..., this is your question how to normalize a user and his friends data base.

My View, you need three tables

Individual (IndId (PK), Name ...)
User (UserId (PK), IndId (FK to Individual Table), login, password etc)
Friends(FID(PK), UserId (FK to User Table), IndId (FK to Individual Table))

Get all friends of a user

Select I.* from Friends F
     JOIN User U on F.UserId=U.UserId // Get all friends of all users
     JOIN Individual I on I.IndId =F.IndId // Get there names etc
   Where I.Name = 'MyFriend' // filter a friend

相关问答

更多
  • 当我们说SQL Server然后我们说RDBMS。 然后有规范化 INF,2NF和3rdNF ......,这是你如何规范用户和他的朋友数据库的问题。 我的观点,你需要三张桌子 Individual (IndId (PK), Name ...) User (UserId (PK), IndId (FK to Individual Table), login, password etc) Friends(FID(PK), UserId (FK to User Table), IndId (FK to Indi ...
  • https://developers.facebook.com/docs/apps/faq#invite_to_app 你不能再和/me/friends ,但如果你的App是Facebook Canvas上的游戏,你可以使用/me/invitable_friends 。 顺便说一下,你不需要额外批准user_friends , read_friendlists只是为了获取列表 - 没有任何朋友。 绝对不是你想要的。 如果您想要申请其他权限,或者您希望您的游戏在Facebook上的App Center中显示, ...
  • 无需存储朋友ID,因为您可以从Facebook获取此信息(通过在friend桌上运行FQL)。 只需存储当前登录的用户ID(可能需要在用户表上使用last_seen时间戳,然后搜索在过去15分钟内与应用程序交互的用户)。 所以,像这样: 每当用户与应用更新用户的last_seen时间戳进行last_seen时 在用户登录时通过FQL检索朋友ID并将其存储在会话中,如果您不想在每个网页浏览中检索此列表(朋友列表很可能在登录会话期间不会更改) 在需要时在当前活动用户中搜索朋友ID No need to stor ...
  • 如果需要存储大量数据,则需要存储大量数据。 如果你最喜欢,你可能不会比现金解决它更快遇到这个问题。 换句话说,您可能假设您拥有的流量和数据将超过您的流量和数据,至少在短期内如此。 所以我怀疑这是一个问题,尽管这是一个很好的迹象,表明你现在而不是后来考虑它。 正如我在下面的评论中提到的,最简单的解决方案是在朋友关系的每一侧都有一个连接表(一个has_many :friends, through: :facebook_friend_relationships, class_name: 'FacebookFrie ...
  • 不,当用户授权应用程序时,共享的最小数据是它的基本信息和朋友列表。 不幸的是你无法改变这一点。 官方文件: 当有人使用Facebook登录与应用程序连接时,该应用程序可以访问他们的公开个人资料和朋友列表 ,这些信息对每个人都是可见的。 某人添加到他们的Facebook个人资料中的其他信息都是在权限背后保护的。 来源:Facebook No, when a user authorize an application the minimum data shared is it's basic informati ...
  • 对于这种情况,没有办法让所有朋友了。 您只能通过/me/friends获得授权您的应用的用户。 在这个帖子的答案中解释了其他一切: Facebook Graph Api v2.0 + - / me / friends返回空,或者只有使用我的应用程序的朋友 如果你明白这一点,那么我不确定你的问题是什么,说实话。 如果你想创建自己的朋友列表,你可以这样做,但没有Facebook API。 For that case, there is no way to get all friends anymore. You ...
  • 这是低效的。 你在关系方面的含义是用户和游戏之间的多对多关系。 用户可以在许多游戏上投票。 游戏可以被许多用户投票。 解决方案是有一个连接表: USERS (uid, name, ...) GAMES (gid, name, ...) VOTES (id, uid, gid, ...) 其中uid和gid是外键返回到它们各自的表格。 如果有人投票,请在VOTES中插入一条记录。 要获得游戏的选票列表,请执行以下操作: $get = mysql_query("SELECT * FROM votes WHER ...
  • Facebook中有隐私设置,可以将朋友列表限制为仅限朋友。 由于您的应用不是您尝试访问的用户ID的朋友,因此您受到限制。 您可以通过在新的浏览器窗口(新会话)中尝试配置文件URL来检查它。 看起来有问题的用户ID无法访问好友列表。 http://www.facebook.com/profile.php?id=100001150965395 There is privacy setting in Facebook which can restrict Friends list to Friends alon ...
  • 假设您的帖子ID对他们有某种顺序(并且用户按顺序对每个帖子进行投票),您应该只需要存储最近添加的帖子的ID。 这样你可以加载下一个20个帖子ID。 如果用户可以对不按顺序发布的帖子进行投票,则可以存储他们投票的帖子部分。 (例如,user1已经对帖子3,4,5,6,9,10进行了投票,因此您存储了数字对3,6和9,10,表明所有帖子之间的所有帖子都已被投票,从而减少了您使用的空间量。) 或者,如果您在发布帖子时存储了日期和时间,则可以跟踪用户投票的最后一个帖子的日期和时间。 从这里你应该能够将阵列所需的大小 ...
  • 扩展我上面的评论,因为它变得罗嗦: 创建一个名为user_follows的新表,其中包含列 user_id1 | user_id2 要么 follower_id | follows_id 然后你可以查询: SELECT t1.username as follower_username, t3.username as following_usernae FROM users t1 INNER JOIN user_follows t2 ON t1.user_id = t2.follower_id ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)