简体   繁体   English

在数组对象中查找值并使用该对象中的其他值

[英]Find a value within array objects and use another value from that object

Can't find a the correct workflow to get the following from looping thro array: 无法找到正确的工作流程以从循环thro数组中获取以下内容:

  • loop thro all the objects within the array and find a specific warehouse value 循环遍历数组中的所有对象并查找特定的warehouse
  • if the value exists in any object, use that object which holds the specific warehouse value and return a price value within it 如果该值存在于任何对象中,请使用包含特定warehouse值的对象并在其中返回price

For example: If I would do a search for 1374610389 warehouse value I would get returned price of 78.00 例如:如果我要搜索1374610389仓库价值,我会得到78.00退货价格

 var test = [ { lorem: "ipsum", dolor: "sit", price: "10.00", warehouse: 1157964289 }, { lorem: "ipsum", dolor: "sit", price: "22.00", warehouse: 1269753487 }, { lorem: "ipsum", dolor: "sit", price: "78.00", warehouse: 1374610389 }, { lorem: "ipsum", dolor: "sit", price: "32.00", warehouse: 1674985630 }, { lorem: "ipsum", dolor: "sit", price: "16.00", warehouse: 1847893458 } ] 

The find() method returns a value of the first element in the array that satisfies the provided testing function. find()方法返回数组中第一个满足提供的测试函数的元素的值。

You could use the find method : 你可以使用find方法:

test.find(obj => obj.warehouse == '1374610389').price

 var test = [ { lorem: "ipsum", dolor: "sit", price: "10.00", warehouse: 1157964289 }, { lorem: "ipsum", dolor: "sit", price: "22.00", warehouse: 1269753487 }, { lorem: "ipsum", dolor: "sit", price: "78.00", warehouse: 1374610389 }, { lorem: "ipsum", dolor: "sit", price: "32.00", warehouse: 1674985630 }, { lorem: "ipsum", dolor: "sit", price: "16.00", warehouse: 1847893458 } ] console.log( test.find(obj => obj.warehouse == '1374610389').price); 

var test = [{
  lorem: "ipsum",
  dolor: "sit",
  price: "10.00",
  warehouse: 1157964289
}, {
  lorem: "ipsum",
  dolor: "sit",
  price: "22.00",
  warehouse: 1269753487
}, {
  lorem: "ipsum",
  dolor: "sit",
  price: "78.00",
  warehouse: 1374610389
}, {
  lorem: "ipsum",
  dolor: "sit",
  price: "32.00",
  warehouse: 1674985630
}, {
  lorem: "ipsum",
  dolor: "sit",
  price: "16.00",
  warehouse: 1847893458
}];
for (var i = 0; i < test.length; i++) {
  if (test[i].warehouse == 1374610389) {
    console.log(test[i].price);
  }
}

Get the length of the array using the length propertie and enumerate over it using a for . 使用长度属性获取数组的长度,并使用for枚举。

Also check Zakaria Acharki answer, his is way more elegant. 还检查Zakaria Acharki的回答,他的方式更优雅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用另一个对象数组更改一个对象数组中的对象值? - How to change object value within an array of objects with one from another array of objects? 将键/值数组中的对象与另一个对象(键/值)进行比较 - Comparing objects from key/value array with another object (Key/value) 使用属性值从对象数组中查找并返回 object - Find and Return an object from an array of array of objects using a property value 您将如何使用 map 从对象数组中的每个对象中查找特定值 - How would you use map to find a specific value from each object in an array of objects 如何根据另一个具有 id 的 arrays 从对象数组中查找值 - how to find a value from an array of objects based on another arrays with ids 过滤对象内object的数组值 - Filtering array value of object within objects 如何使用另一个对象中的值过滤对象数组? - How can I filter an array of objects with a value from another object? 将 object 值从一个对象数组移动到另一个对象数组 - Move object value from one array of objects to another 更改对象中值的状态,在对象数组中的索引处,在对象中? - Changing state of a value in an object, at an index within an array of objects, within a object? 获取同一对象中另一个值的JSON对象值 - Aquiring JSON objects value for another value within same object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM