简体   繁体   English

Javascript对象数组:返回值的最佳方法?

[英]Javascript array of objects: Best way to return a value?

I have an array of objects in Javascript with two keys with this structure: 我有一个Javascript对象数组,带有两个具有此结构的键:

"data": [
  {
  "description": "Unknown",
  "type": 0
  },
  {
  "description": "On",
  "type": 1
  },
  {
  "description": "Off",
  "type": 2
  },
  ...
  ]

I want to pass it a 'type' numeric value and if it finds it in the array, returns me the description value. 我想给它传递一个“类型”的数值,如果它在数组中找到它,就向我返回描述值。 For example, if I pass '0', I want it to return 'Unknown'. 例如,如果我传递“ 0”,我希望它返回“未知”。

This is easily done with a for or forEach loop, but there is an inbuilt function in JS that lets me do it in a single line? 这可以通过for或forEach循环轻松完成,但是JS中有一个内置函数可以让我在一行中完成它?

You could use either find 您可以使用任一find

 var data = [{ description: "Unknown", type: 0 }, { description: "On", type: 1 }, { description: "Off", type: 2 }]; console.log(data.find(({ type }) => type === 1).description); 

or for faster access use a hash table for the types 或为快速访问而对类型使用哈希表

 var data = [{ description: "Unknown", type: 0 }, { description: "On", type: 1 }, { description: "Off", type: 2 }], types = Object.assign(...data.map(({ type, description }) => ({ [type]: description }))); console.log(types[1]); 

or a Map Map

 var data = [{ description: "Unknown", type: 0 }, { description: "On", type: 1 }, { description: "Off", type: 2 }], types = data.reduce((m, { type, description }) => m.set(type, description), new Map); console.log(types.get(1)); 

In one liner you can Filter the list. 在一个衬里中,您可以过滤列表。

 var obj=[{ "description": "Unknown", "type": 0 }, { "description": "On", "type": 1 }, { "description": "Off", "type": 2 }]; var filterType=1; console.log(obj.filter(e=>e.type == filterType)[0]['description']) 

Have a look at the attached code snippet , which gives the desired output. 查看附带的代码段,它给出了所需的输出。

  • Find function to itreat throughout your array to match the condition ie in this case the passedIndex to check whether it is present in list 查找函数遍历整个数组以匹配条件,例如,在这种情况下,passedIndex将检查它是否存在于列表中

  • If present return the item 如果存在,请退回该物品

 var list = [{ "description": "Unknown", "type": 0 }, { "description": "On", "type": 1 }, { "description": "Off", "type": 2 } ]; function findItem(index) { var result = list.find(function(item) { if (item.type == index) { return item; } }); return result.description; } var description = findItem(2); console.log('The Description is "' + description + '"') 

.

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

相关问题 访问对象数组中的键、值对的最佳方法? 在 JavaScript 中 - Best way to access key, value pair inside of an array of objects? in javascript Javascript:实例化对象数组的最佳方法 - Javascript: best way to instantiate an array of objects 在Javascript中创建对象数组的最佳方法是什么? - What is the best way to create an array of objects in Javascript? 返回数组键的关于其中一个包含对象的值的最佳方法 - Best way to return the key of an array about the value of one of its containing objects 在对象数组中查找最小值并返回其他属性的最佳方法? - Best way to find min value in array of objects and also return the other property? 以函数方式从对象数组中的对象数组返回值 - Return value from array of objects inside an array of objects the functional way 在JavaScript中存储key => value数组的最佳方法? - Best way to store a key=>value array in JavaScript? 在javascript中搜索对象数组以查找另一个数组的最佳方法是什么? - What is the best way to search an array of objects to find another array in javascript? 从对象数组中查找对象索引的最佳方法是什么 - javascript - What is the best way to find index of object from array of objects - javascript 在javascript中更改对象数组顺序的最佳方法是什么 - What is the best way to change the order of array of objects in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM