通过属性值从对象数组中获取JavaScript对象[重复]
本文翻译自:Get JavaScript object from array of objects by value of property [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
- Find object by id in an array of JavaScript objects 32 answers 在JavaScript对象数组中按ID查找对象 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楼
参考:https://stackoom.com/question/waiJ/通过属性值从对象数组中获取JavaScript对象-重复
#2楼
Filter
array of objects, which property matches value, returns array: Filter
对象数组,其属性与值匹配,返回数组:
var result = jsObjects.filter(obj => {return obj.b === 6
})
See the MDN Docs on Array.prototype.filter() 请参阅Array.prototype.filter()上的MDN文档
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 MDN Docs on Array.prototype.find() 请参阅Array.prototype.find()上的MDN文档
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<iLen; i++) {if (arr[i].b == value) return arr[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<iLen; i++) {o = arr[i];for (var p in o) {if (o.hasOwnProperty(p) && o[p] == value) {return o;}}}
}
#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,您可以将其包括在内以在所有浏览器中获得其功能。
find()
: find()
:
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 found
console.log( [4, 5, 8, 12].find(isPrime) ); // 5
findIndex()
: findIndex()
:
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 found
console.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;
通过属性值从对象数组中获取JavaScript对象[重复]相关推荐
- 从JavaScript数组中获取随机项[重复]
本文翻译自:Get random item from JavaScript array [duplicate] This question already has answers here : 这个问 ...
- jQuery过滤器:筛选jquery对象数组中的DOM对象
目录 一.定义: 二.基本过滤器: 一.定义: 过滤器就是过滤条件,对已经定位到jquery对象数组中的DOM对象进行过滤筛选,过滤条件不能独立出现在jquery函数,如果使用则只能出现在选择器后方. ...
- java定时器任务中获取request对象 @Scheduled 获取request 对象 quartz中获取request对象
java定时器任务中获取request对象 @Scheduled 获取request 对象 quartz中获取request对象 一.问题描述 1.在java定时任务中,使用 @Scheduled 注 ...
- js数组查找最接近_如何从javascript中的对象数组中获取最接近的先前id
我对您的代码进行了一些更改,现在应该可以正常工作了.看一看. const array = [{id:3}, {id:4}, {id:10}, {id:15}]; // you should order ...
- JS对象数组中依据某个属性的值分组
问题:需要在对象数组中根据某个属性值进行归类或者分组 const dataList = [{name: 'Apple',type: 'Fruit',price: '¥1.5'},{name: 'Ban ...
- js 加总数组中某一列_js根据对象数组中某一属性值,合并相同项,并对某一属性累加处理...
js根据对象数组中某一属性值,合并相同项,并对某一属性累加处理 Example: 根据code合并数组,并将sl值累加,如下两种方法: let data = [{ code: 1001, name: ...
- map中获取数组_如何从php多维数组中获取特定的键值?
点击蓝字关注我们!每天获取最新的编程小知识! 源 / php中文网 源 / www.php.cn 在这篇文章中,我们将给大家介绍如何在php中从多维数组中获取特定的键值数组.这里我们将使用a ...
- es6 数组找最大值_在对象数组中查找属性的最大值
我正在寻找一种非常快速,干净且有效的方法来获取以下JSON切片中的最大" y"值: [ { "x": "8/11/2009", " ...
- 如何通过其值获取JavaScript对象中的键?
本文翻译自:How to get a key in a JavaScript object by its value? I have a quite simple JavaScript object, ...
最新文章
- 用coffee和socket.io实现的01背包算法
- 太残忍!麦当劳用毒气室杀鸡
- 教授先生带你学习链表:链表节点的删除与增添2
- 人生七大纲要——道、德、仁、义礼、智、信
- 非因解读|Digital Spatial Profiler 新一代高维度空间组学技术
- 安全浏览器版本过低?该升级浏览器内核了
- 哈夫曼编码问题(贪心)
- app live photo_live photo动态壁纸下载-Live Photo动态壁纸app下载 苹果版v2.1-PC6苹果网...
- 全国市场调查大赛经验分享(二)
- 清华大学计算机专业辅修课程,清华大学计算机应用专业-辅修专业
- 点击链接重定向跳转微信公众号关注页、微信关注链接
- ad17编辑界面怎么检查未连线_EZCast投屏软件提示未发现装置,请检查网路连线状态怎么办?...
- 一分钟了解“查看一台windows电脑是否成功安装了CUDA”
- Universal Termsrv.dll Patch 是个好东西
- Word 2013难搞的页眉,如何把单独一页的页眉去掉?
- Tungsten Fabric SDN — 与 Kubernetes 的集成部署(CN)
- stm32自定义usb_HID设备
- 【场景化解决方案】构建门店通讯录,“门店通”实现零售门店标准化运营
- 以太坊·电影院场景区块链应用探索
- SDL_UpdateTexture+ffmpeg播放YUV数据程序异常崩溃:VM Regions Near 0x113e3d000:MALLOC_LARGE
热门文章
- Android背景渐变色(shape,gradient)
- 安卓开发重磅炸弹!程序员福利!《高级Kotlin强化实战学习手册(附Demo)》开放下载!
- 【脚下生根】之深度探索安卓OpenGL投影矩阵
- Glide @GlideModule 注解使用
- 【剑指offer-Java版】35第一个只出现一次的字符
- kotlin的Delegates与lateinit对比
- Java、Android注解代码生成(ButterKnife原理、ViewBinding)
- 好想学python怎么猜人名_猜人名的谜语大全及答案
- 灰度值怎么降级_快速提取照片中间调 用灰度蒙版为照片调色 得到更柔和的后期效果...
- 【Effective Java】1.静态工厂方法来替换构造函数