搜索JS对象的值(Search JS Object for Value)

编程入门 行业动态 更新时间:2024-10-27 08:35:37
搜索JS对象的值(Search JS Object for Value)

说我有一个对象:

userInfo

我想搜索userInfo的每个节点,看看键'username'的值是否等于foo。

userInfo[x].username == "foo"

有更好的方法来做以下事情吗?

var matchFound = false; for (var i = 0, len = userInfo.length; i < len; i++) matchFound = userInfo[i].username == "foo";

Say I have an object:

userInfo

And I want to search each node of userInfo to see if the key 'username' has a value equal to foo.

userInfo[x].username == "foo"

Is there a better way of doing the following?

var matchFound = false; for (var i = 0, len = userInfo.length; i < len; i++) matchFound = userInfo[i].username == "foo";

最满意答案

没有引入另一种数据结构,没有更好的(更有效的)方法。 答案真的取决于你的用法,但你可以做一些不同的事情:

使用哈希创建单独的“索引”。 这些结构将键映射到源数组中的项或索引。 JavaScript对象/哈希支持基于键的查找,应该是高效的。

userinfo[x].username = "foo"; // Index the objects usersByName = {}; usersByName["foo"] = userinfo[x]; // -- OR -- index the array indices var usersByName["foo"] = x; // Test for key "foo" in usersByName; // true

您将不得不投入更多工作来维护索引和源数组之间的一致性。 最好将两者都包装在另一个对象中以管理两者的内容。 如果有多个字段要查看对象,则此方法很好。

如果您不关心集合的顺序,您可以通过用户名将整个内容更改为哈希和索引

var userinfo = {}; userinfo["foo"] = {username: "foo", firstName: "Foo", lastName: "Bar"};

但要考虑的一件事是,效率增益是否会超过维护索引所增加的代码复杂性。 如果您没有进行大量搜索,并且在userinfo集合中没有大量项目,那么编写一般用途搜索功能或使用像Philip Schweiger所提到的库这样的库可能更有意义。

function findObjectByAttribute (items, attribute, value) { for (var i = 0; i < items.length; i++) { if (items[i][attribute] === value) { return items[i]; } } return null; } var userinfo = []; userinfo[0] = {username: "foo"}; console.log(findObjectByAttribute(userinfo, "username", "foo"));

There isn't really a better (more efficient) way without introducing another data structure. The answer really depends on your usage but you could do a few different things:

Create separate 'indexes' using hashes. These structures would map keys to the items or the index in the source array. JavaScript objects/hashes support key based lookup and should be efficient.

userinfo[x].username = "foo"; // Index the objects usersByName = {}; usersByName["foo"] = userinfo[x]; // -- OR -- index the array indices var usersByName["foo"] = x; // Test for key "foo" in usersByName; // true

You'll have to put in a little more work to maintain consistency between the index and the source array. It's probably best to wrap both in another object to manage the contents of both. This approach is nice if there are multiple fields that you want to look objects up by.

If you don't care about the order of the collection you could just change the whole thing to a hash and index by username

var userinfo = {}; userinfo["foo"] = {username: "foo", firstName: "Foo", lastName: "Bar"};

One thing to think about, though, is if the efficiency gains are going to outweigh the increased code complexity of maintaining indexes. If you aren't doing a lot of searches and you don't have tons of items in the userinfo collection it may make more sense to just write a general use searching function or use a library like what Philip Schweiger was mentioning.

function findObjectByAttribute (items, attribute, value) { for (var i = 0; i < items.length; i++) { if (items[i][attribute] === value) { return items[i]; } } return null; } var userinfo = []; userinfo[0] = {username: "foo"}; console.log(findObjectByAttribute(userinfo, "username", "foo"));

更多推荐

本文发布于:2023-07-22 01:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1215526.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   JS   Object   Search

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!