是否有更有效的方法来选择对象数组中的特定对象

发布于 2024-11-08 20:22:58 字数 631 浏览 0 评论 0原文

给定以下数据结构

var things = [{ "name": "thing1", "sex": "male"},
              { "name": "thing2", "sex": "female"}];

,我希望能够搜索该对象数组并提取特定对象。

我目前编写的 JsFiddle 代码

function selectElementByName (name) { 

  var returnObject;

  for (var i = 0; i < things.length; i++) {

    if (things[i].name === name) {

        returnObject = things[i];
    }
  } 


  if ( returnObject === undefined) { 
    console.log("Object not found");
  }

  return returnObject;
}

可以在 这里 找到,

有没有更有效的方法这?

Given the following data structure

var things = [{ "name": "thing1", "sex": "male"},
              { "name": "thing2", "sex": "female"}];

I would like to be able to search that array of objects and pull out a particular object.

I currently have the following code written

function selectElementByName (name) { 

  var returnObject;

  for (var i = 0; i < things.length; i++) {

    if (things[i].name === name) {

        returnObject = things[i];
    }
  } 


  if ( returnObject === undefined) { 
    console.log("Object not found");
  }

  return returnObject;
}

JsFiddle can be found here

Is there a more efficient way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑红尘 2024-11-15 20:22:58

您可以在找到对象时提前退出,这样您就不必循环遍历数组的其余部分:(

function selectElementByName(name) { 
  for (var i = 0; i < things.length; i++) {
    if (things[i].name === name) {
      return things[i];
    }
  }
  console.log("Object not found");
}

但是,如果存在重复项,这将改变行为,以便它返回找到的第一个对象而不是最后一个。)


如果名称是唯一的,您可以使用它们作为键并将对象存储在对象中而不是数组中:

var things = {
  "thing1": { "name": "thing1", "sex": "male"},
  "thing2": { "name": "thing2", "sex": "female"}
};

那么您不需要循环来查找对象:

function selectElementByName(name) { 
  return things[name];
}

如果您需要数组中的对象,如果数组没有改变,你仍然可以创建一个索引来搜索经常使用:

var thingsNameIndex = {};
for (var i = 0; i < things.length; i++) {
  thingsNameIndex[things[i].name] = i;
}

现在您可以使用索引来查找对象:

function selectElementByName(name) { 
  return things[thingsNameIndex[name]];
}

由于数组更改后您必须立即更新或重新创建索引,因此只有当您在数组中搜索的频率比更改数组的频率高得多时,这才有用。

You can make an early exit when an object is found, so that you don't have to loop through the rest of the array:

function selectElementByName(name) { 
  for (var i = 0; i < things.length; i++) {
    if (things[i].name === name) {
      return things[i];
    }
  }
  console.log("Object not found");
}

(This will however change the behaviour if there are duplicates, so that it returns the first object found instead of the last.)


If the names are unique, you could use them as key and store the objects in an object instead of in an array:

var things = {
  "thing1": { "name": "thing1", "sex": "male"},
  "thing2": { "name": "thing2", "sex": "female"}
};

Then you wouldn't need to loop to find an object:

function selectElementByName(name) { 
  return things[name];
}

If you need the objects in an array, you could still create an index for searching if the array doesn't change so often:

var thingsNameIndex = {};
for (var i = 0; i < things.length; i++) {
  thingsNameIndex[things[i].name] = i;
}

Now you can use the index to find the object:

function selectElementByName(name) { 
  return things[thingsNameIndex[name]];
}

As you have to update or recreate the index as soon as the array changes, this would only be usedful if you do searches in the array much more often than you change the array.

星星的軌跡 2024-11-15 20:22:58

在最新版本的 JavaScript 中:

var selectElementByName = function(name) {
    // This will fetch all items that match the filter
    // and place them in a new array
    var items = things.filter(function(item) {
        return item.name === name;
    });

    // If there isn't anything in the array, log the error and return.
    if (!items.length) {
        console.log("Object not found");
        return;
    }

    // Return the first matched element in the array.
    return items[0];
};

In more recent versions of JavaScript:

var selectElementByName = function(name) {
    // This will fetch all items that match the filter
    // and place them in a new array
    var items = things.filter(function(item) {
        return item.name === name;
    });

    // If there isn't anything in the array, log the error and return.
    if (!items.length) {
        console.log("Object not found");
        return;
    }

    // Return the first matched element in the array.
    return items[0];
};
最初的梦 2024-11-15 20:22:58

一旦找到至少你可以打破:

for (var i = 0; i < things.length; i++) {
    if (things[i].name === name) {                        
        returnObject = things[i];
        break;
    }
} 

You can break once it's found at least:

for (var i = 0; i < things.length; i++) {
    if (things[i].name === name) {                        
        returnObject = things[i];
        break;
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文