【发布时间】:2015-12-08 01:16:45
【问题描述】:
在给定对象数组的多个属性的情况下,获取对象数组中对象索引的最佳方法是什么?
想象以下示例数组:
var array = [
{
color: 'red',
shape: 'square',
weight: 200
},
{
color: 'blue',
shape: 'circle',
weight: 300
},
{
color: 'red',
shape: 'circle',
weight: 100
}
];
现在我想要indexOf 的对象,其color 属性为red,shape 为circle,在本例中为2。
理想情况下,当对象的属性子集如{color: 'red', shape: 'circle'} 给出时,该函数将返回对象的索引,如果未找到索引,则返回-1。
【问题讨论】:
-
@JoelEtherton 我尝试的是循环数组,然后循环遍历属性,但我被卡住的部分是只获取与这两个值匹配的项目的索引。 IndexOf 只是
colorred与shapecircle不同。
标签: javascript arrays javascript-objects indexof