admin管理员组文章数量:1401589
I am currently looking up objects in an array using one value, item number. However, if there happened to be multiple orders that both had the same item number on them, how could the particular object index be looked up using both values?
Object is structured in such a manner:
var object = {
line: line,
poNumber: purchaseOrder,
item: item
};
Here is how I am looking the objects up now:
var posArrInd = posArr.map(function (x) { return x.item; }).indexOf(String(item));
var po = posArr[posArrInd];
var poLine = po.line;
I am currently looking up objects in an array using one value, item number. However, if there happened to be multiple orders that both had the same item number on them, how could the particular object index be looked up using both values?
Object is structured in such a manner:
var object = {
line: line,
poNumber: purchaseOrder,
item: item
};
Here is how I am looking the objects up now:
var posArrInd = posArr.map(function (x) { return x.item; }).indexOf(String(item));
var po = posArr[posArrInd];
var poLine = po.line;
Share
Improve this question
asked Jan 15, 2019 at 13:28
J.J.J.J.
1,1182 gold badges28 silver badges64 bronze badges
2
- So, to clarify - you have an array of objects. You want to find all indexes of those objects matching some condition and you can have more than one. Is that correct? Also, do you have to find indexes or can you work with just the objects? – VLAZ Commented Jan 15, 2019 at 13:31
- There should only be one object that has a particular order number and item number, however, if just looking up objects by item number there could be multiple objects that are matched, so that is why I want to add the purchase order number to the criteria as well. Indexes or directly working with the objects are both fine. – J.J. Commented Jan 15, 2019 at 13:34
2 Answers
Reset to default 4ES6+
You can use .findIndex()
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
var found = items.findIndex(function(itm) {
return itm.number1 === number1 && itm.number2 === number2;
});
ES5:
Using .filter():
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
var foundItems = items.filter(function(itm) {
return itm.number1 === number1 && itm.number2 === number2;
});
if (foundItems && foundItems.length > 0) {
var itemYouWant = foundItems[0];
}
Getting the index--you can have the index value returned as part of the filter method. Check out the docs for more examples.
Sounds like you could just use filter
if I am reading your question correctly. For example:
var theItem = 'however your item numbers look'
var matches = posArr.filter(function(x) { return x.item === theItem })
This will return an array of all things in posArr
that have a particular item number specified in theItem
本文标签: javascriptHow to find index of object in array using multiple values with ES5Stack Overflow
版权声明:本文标题:javascript - How to find index of object in array using multiple values with ES5? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744251173a2597256.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论