如何通过键在对象数组中获取值

时间:2019-11-27 07:16:42

标签: arrays angular typescript

如何通过键(也位于此对象数组中)在对象数组中获取值。

对象数组如下:

const objectArray = [
    {key: "1", value: "12321"},
    {key: "2", value: "asdfas"}
]

我现在有了key的值,例如key = 1,但我想得到12321。

任何解决方案?

4 个答案:

答案 0 :(得分:3)

您可以使用.find()来实现。

尝试一下:

Working Demo

this.objectArray.find(x => x.key == "1").value

要处理异常,如果数组中不存在该项,请执行以下操作:

let item = this.objectArray.find(x => x.key == "1")
this.value = item ? item.value : null

答案 1 :(得分:0)

javax.validation.ValidationException: HV000041: Call to TraversableResolver.isReachable() threw an exception.
at org.hibernate.validator.internal.engine.ValidatorImpl.isReachable(ValidatorImpl.java:1620) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.isValidationRequired(ValidatorImpl.java:1596) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:608) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:581) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:527) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:495) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:460) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:410) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:207) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
at com.nucleus.custom.validator.ApplicationUploadValidator.validationcheck(ApplicationUploadValidator.java:89) ~[neo-custom-4.00.02.00-GA.jar:na]
at com.nucleus.custom.validator.ApplicationUploadValidator.uploadAppSubvDetail(ApplicationUploadValidator.java:54) ~[neo-custom-4.00.02.00-GA.jar:na]
at com.nucleus.custom.service.UploadServiceImpl.ApplicationUploadProcessing(UploadServiceImpl.java:52) ~[neo-custom-4.00.02.00-GA.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95) ~[na:1.8.0]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55) ~[na:1.8.0]
at java.lang.reflect.Method.invoke(Method.java:495) ~[na:2.6 (01-16-2015)]
at org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:278) [commons-beanutils-1.9.1.jar:1.9.1]
at org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:222) [commons-beanutils-1.9.1.jar:1.9.1]
at org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:173) [commons-beanutils-1.9.1.jar:1.9.1]
at com.nucleus.finnone.pro.fileconsolidator.businessobject.UploadTask.call(UploadTask.java:115) [file-consolidator-4.00.02.00-GA.jar:na]
at com.nucleus.finnone.pro.fileconsolidator.businessobject.UploadTask.call(UploadTask.java:45) [file-consolidator-4.00.02.00-GA.jar:na]
at com.nucleus.finnone.pro.fileconsolidator.businessobject.UploadTask$$FastClassBySpringCGLIB$$cd39ec96.invoke(<generated>) [file-consolidator-4.00.02.00-GA.jar:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) [spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) [spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$$Lambda$216.00000000A606D9D0.proceedWithInvocation(Unknown Source) ~[na:na]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) [spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at com.nucleus.finnone.pro.fileconsolidator.businessobject.UploadTask$$EnhancerBySpringCGLIB$$4d97f095.call(<generated>) ~[file-consolidator-4.00.02.00-GA.jar:na]
at java.util.concurrent.FutureTask.run(FutureTask.java:277) ~[na:1.8.0]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:522) ~[na:1.8.0]
at java.util.concurrent.FutureTask.run(FutureTask.java:277) ~[na:1.8.0]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153) ~[na:1.8.0]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:1.8.0]
at java.lang.Thread.run(Thread.java:785) ~[na:1.8.0]
Caused by: java.lang.ClassCastException: org.eclipse.persistence.jpa.PersistenceProvider incompatible with javax.persistence.spi.PersistenceProvider
at javax.persistence.Persistence$1.isLoaded(Persistence.java:110) ~[hibernate-jpa-2.1-api-1.0.0.Final.jar:1.0.0.Final]
at org.hibernate.validator.internal.engine.resolver.JPATraversableResolver.isReachable(JPATraversableResolver.java:47) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver.isReachable(DefaultTraversableResolver.java:128) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation.isReachable(CachingTraversableResolverForSingleValidation.java:36) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.isReachable(ValidatorImpl.java:1611) ~[hibernate-validator-5.4.0.Final.jar:5.4.0.Final]
... 36 common frames omitted

答案 2 :(得分:0)

您可以使用filter()并使用您已经拥有的键的值来完成此操作。

const objectArray = [
    {key: "1", value: "12321"},
    {key: "2", value: "asdfas"}
]

const el = objectArray.filter(item => item.key == 1)[0];

el
  ? console.log(el.value) // gives 12321
  : console.log('none listed')

答案 3 :(得分:0)