简体   繁体   English

如何检查离子3中是否存在数组值

[英]How to check if array value exists in ionic 3

I'm new to ionic 3. I'm geting an array which is selected from dropdown option: {"3":"5","4":"7"} . 我是离子3的新手。我正在创建一个从下拉选项中选择的数组:{“3”:“5”,“4”:“7”}

now i need to check the dropdown value with this: 现在我需要检查下拉值:

"variant_json": "{\"14\":\"5,7\",\"15\":\"5,8\",\"16\":\"6,7\",\"17\":\"6,8\"}"

For Example: 例如:

In dropdown, if i select 5,7 means it should check from "variant_json" and need to show its key is 14 . 在下拉列表中,如果我选择5,7表示它应该从“variant_json”检查并且需要显示其键是14

You can use Object.keys and find 您可以使用Object.keys查找

 let json = JSON.parse("{\\"14\\":\\"5,7\\",\\"15\\":\\"5,8\\",\\"16\\":\\"6,7\\",\\"17\\":\\"6,8\\"}") let findKeyByValue = (value) =>{ let found = Object.keys(json).find((key)=>json[key] === value) return found ? found : 'Not found' } console.log(findKeyByValue('5,7')) console.log(findKeyByValue('5,8')) console.log(findKeyByValue('9,9')) 

Use Object.entries : 使用Object.entries

 const obj = { "variant_json": "{\\"14\\":\\"5,7\\",\\"15\\":\\"5,8\\",\\"16\\":\\"6,7\\",\\"17\\":\\"6,8\\"}" }; const [, key ] = Object.entries(obj.variant_json).find(([k, v]) => v == "5,7"); console.log(key); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM