Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在ES5中使用多个值查找数组中对象的索引?_Javascript_Ecmascript 5 - Fatal编程技术网

Javascript 如何在ES5中使用多个值查找数组中对象的索引?

Javascript 如何在ES5中使用多个值查找数组中对象的索引?,javascript,ecmascript-5,Javascript,Ecmascript 5,我目前正在使用一个值item number查找数组中的对象。但是,如果碰巧有多个订单上都有相同的项目编号,那么如何使用这两个值查找特定的对象索引 对象的结构如下所示: var object = { line: line, poNumber: purchaseOrder, item: item }; 下面是我现在查找对象的方式: var posArrInd = posArr.map(function (x) { return x.item; }).indexOf(Stri

我目前正在使用一个值item number查找数组中的对象。但是,如果碰巧有多个订单上都有相同的项目编号,那么如何使用这两个值查找特定的对象索引

对象的结构如下所示:

var object = {
    line: line,
    poNumber: purchaseOrder,
    item: item
};
下面是我现在查找对象的方式:

var posArrInd = posArr.map(function (x) { return x.item; }).indexOf(String(item));
var po = posArr[posArrInd];
var poLine = po.line;
ES6+

你可以使用findIndex

ES5:

使用.filter:


获取索引时,可以将索引值作为筛选方法的一部分返回。查看文档了解更多示例。

如果我正确阅读了您的问题,您可以使用。例如:

 var theItem = 'however your item numbers look'
 var matches = posArr.filter(function(x) { return x.item === theItem })

这将返回posArr中的所有内容的数组,这些内容在ITEEM中指定了特定的项目编号,因此,澄清一下,您有一个对象数组。您希望找到与某个条件匹配的这些对象的所有索引,并且可以有多个索引。对吗?此外,您是否必须查找索引或仅处理对象?应该只有一个对象具有特定的订单号和项目号,但是,如果仅按项目号查找对象,可能会有多个匹配的对象,因此我也希望将采购订单号添加到条件中。索引或直接处理对象都很好。.findIndex不在ES5AH中,您是对的,那么filter将以相同的方式工作,但使用添加的and语句,您只需要期望返回一个包含一个项的数组。short:posArr.filterx=>x.item==theItem@KamilKiełczewski问题特别指出了es5,这就是为什么我没有使用箭头函数。
var foundItems = items.filter(function(itm) {
   return itm.number1 === number1 && itm.number2 === number2;
});

if (foundItems && foundItems.length > 0) {
 var itemYouWant = foundItems[0];
}
 var theItem = 'however your item numbers look'
 var matches = posArr.filter(function(x) { return x.item === theItem })