博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过属性值从对象数组中获取JavaScript对象[重复]
阅读量:2289 次
发布时间:2019-05-09

本文共 3569 字,大约阅读时间需要 11 分钟。

本文翻译自:

This question already has an answer here: 这个问题已经在这里有了答案:

  • 32 answers 32个答案

Let's say I have an array of four objects: 假设我有四个对象组成的数组:

var jsObjects = [   {a: 1, b: 2},    {a: 3, b: 4},    {a: 5, b: 6},    {a: 7, b: 8}];

Is there a way that I can get the third object ( {a: 5, b: 6} ) by the value of the property b for example without a for...in loop? 有没有一种方法可以通过属性b的值获取第三个对象( {a: 5, b: 6} ),例如,没有for...in循环?


#1楼

参考:


#2楼

Filter array of objects, which property matches value, returns array: Filter对象数组,其属性与值匹配,返回数组:

var result = jsObjects.filter(obj => {  return obj.b === 6})

See the 请参阅的

const jsObjects = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ] let result = jsObjects.filter(obj => { return obj.b === 6 }) console.log(result)

Find the value of the first element/object in the array, otherwise undefined is returned. Find数组中第一个元素/对象的值,否则返回undefined

var result = jsObjects.find(obj => {  return obj.b === 6})

See the 请参阅的

const jsObjects = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ] let result = jsObjects.find(obj => { return obj.b === 6 }) console.log(result)


#3楼

If I understand correctly, you want to find the object in the array whose b property is 6 ? 如果我理解正确,您想在b属性为6的数组中找到对象。

var found;jsObjects.some(function (obj) {  if (obj.b === 6) {    found = obj;    return true;  }});

Or if you were using underscore: 或者,如果您使用下划线:

var found = _.select(jsObjects, function (obj) {  return obj.b === 6;});

#4楼

I don't know why you are against a for loop (presumably you meant a for loop, not specifically for..in), they are fast and easy to read. 我不知道您为什么反对for循环(大概是指for循环,而不是专门针对..in),它们快速且易于阅读。 Anyhow, here's some options. 无论如何,这里有一些选择。

For loop: 对于循环:

function getByValue(arr, value) {  for (var i=0, iLen=arr.length; i

.filter 。过滤

function getByValue2(arr, value) {  var result  = arr.filter(function(o){return o.b == value;} );  return result? result[0] : null; // or undefined}

.forEach .for每个

function getByValue3(arr, value) {  var result = [];  arr.forEach(function(o){if (o.b == value) result.push(o);} );  return result? result[0] : null; // or undefined}

If, on the other hand you really did mean for..in and want to find an object with any property with a value of 6, then you must use for..in unless you pass the names to check. 另一方面,如果您确实的确想使用..in并希望找到具有值为6的任何属性的对象,那么除非您通过名称进行检查,否则必须使用for..in。 eg 例如

function getByValue4(arr, value) {  var o;  for (var i=0, iLen=arr.length; i

#5楼

It looks like in the ECMAScript 6 proposal there are the Array methods find() and findIndex() . 看起来在ECMAScript 6提案中似乎有Array方法find()findIndex() MDN also offers polyfills which you can include to get the functionality of these across all browsers. MDN还提供了polyfill,您可以将其包括在内以在所有浏览器中获得其功能。

:

function isPrime(element, index, array) {    var start = 2;    while (start <= Math.sqrt(element)) {        if (element % start++ < 1) return false;    }    return (element > 1);}console.log( [4, 6, 8, 12].find(isPrime) ); // undefined, not foundconsole.log( [4, 5, 8, 12].find(isPrime) ); // 5

:

function isPrime(element, index, array) {    var start = 2;    while (start <= Math.sqrt(element)) {        if (element % start++ < 1) return false;    }    return (element > 1);}console.log( [4, 6, 8, 12].findIndex(isPrime) ); // -1, not foundconsole.log( [4, 6, 7, 12].findIndex(isPrime) ); // 2

#6楼

var jsObjects = [{a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8}];

to access the third object, use: jsObjects[2]; 要访问第三个对象,请使用: jsObjects[2];

to access the third object b value, use: jsObjects[2].b; 要访问第三个对象b值,请使用: jsObjects[2].b;

转载地址:http://cjcnb.baihongyu.com/

你可能感兴趣的文章
ssh批量登录并执行命令(python实现)
查看>>
Python模块之---Pexpect
查看>>
Python的Pexpect详解
查看>>
Python模块:optparse 处理命令行参数
查看>>
supervisor安装配置与使用
查看>>
全文索引原理及范例
查看>>
Django运行方式及处理流程总结
查看>>
Android社区链接
查看>>
Android Booting
查看>>
Android4.0之init读取init.ic的过程
查看>>
uboot-2011.12移植到S3C2440(二)——点亮LED灯,the very beginning。
查看>>
LeetCode之Median Of Two Sorted Arrays
查看>>
java.util.Stack类简介
查看>>
求职信结尾处常用语
查看>>
申请原因常用语
查看>>
求职信开头处常用语
查看>>
英文求职信(五)
查看>>
英文求职信(四)
查看>>
英文求职信(三)
查看>>
英文求职信(二)
查看>>