简体   繁体   English

通过属性在对象数组中查找值的语法是什么?

[英]What is the syntax to find a value in an array of objects by a property?

What is the syntax to find a value in an array object?在数组对象中查找值的语法是什么?

 var pStData = []; pStData.push({ st: 'WV', geom: 'xxx' }); pStData.push({ st: 'TX', geom: 'yyy' }); var sGeom = pStData.find(pStData => pStData.st == 'TX').geom; console.log(sGeom);

In my code, pStData.find(pStData => pStData.st == 'TX') is undefined .在我的代码中, pStData.find(pStData => pStData.st == 'TX')undefined

Other than no jQuery there at all, you're defining the array as an array of objects.除了根本没有 jQuery 之外,您将数组定义为对象数组。 Define an object like this:像这样定义一个对象:

var pStData = {};
pStData['WV'] = {geom: 'xxx'};
pStData['TX'] = {geom: 'yyy'};

Then you can access simply by然后你可以简单地访问

var sGeom = pStData['WV'].geom;

You can also access it via您也可以通过

var sGeom = pStData.WV.geom;

Of course you don't need to define the object if you're only looking up a string value, for example this would work just as well.当然,如果您只是查找字符串值,则不需要定义对象,例如这也能正常工作。

var pStData = {};
pStData['WV'] = 'xxx';
pStData['TX'] = 'yyy';

However, defining it as an object does future proof your code, should you need to add extra data values later.但是,如果您稍后需要添加额外的数据值,将其定义为对象可以为您的代码提供未来证明。 Never be afraid to do a bit of future proofing, it can save a lot of work later.永远不要害怕做一点未来的打样,它可以在以后节省很多工作。

Use this function:使用这个功能:

 function findObjectInArrayByProperty(array, propertyName, propertyValue) {
      return array.find((o) => { return o[propertyName] === propertyValue });
 }

So, in your case:所以,在你的情况下:

 function findObjectInArrayByProperty(array, propertyName, propertyValue) { return array.find((o) => { return o[propertyName] === propertyValue }); } var pStData = []; pStData.push({ st: 'WV', geom: 'xxx' }); pStData.push({ st: 'TX', geom: 'yyy' }); var resultObj = findObjectInArrayByProperty(pStData, "st", "TX"); console.log(resultObj);

output -> { st: 'TX', geom: 'yyy' }输出 -> { st: 'TX', geom: 'yyy' }

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

相关问题 在对象数组中查找并替换部分属性值 - Find and replace part of a property value in an array of objects 在对象数组中查找最小值和目标属性 - Find minimum value and a target property in an array of objects 在数组中查找具有相同值属性的对象 - Find objects in array with same value property 在对象数组中查找属性值(Javascript) - Find property value in array of objects (Javascript) 在具有多个属性名称和值的嵌套对象数组中查找对象数组 - find in array of objects with nested array of objects with multiple property name and value 查找具有相同属性值的对象并将它们添加到新的数组属性中 - Find objects with same property value and add them in new array property 使用属性值从对象数组中查找并返回 object - Find and Return an object from an array of array of objects using a property value 如何通过嵌套在对象数组的属性中的对象属性的值在对象数组中找到多个索引? - How to find multiple indexes in array of objects by the value of an object property that nests in a property of array of objects? 在 JavaScript 对象数组中查找第一个未使用的属性值 - Find the first unused property value in an array of javascript objects 在对象数组中查找具有 id 属性最大值的对象 - Find object having maximum value for the `id` property in an array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM