如何从Angular中的对象中获取键值对。

huangapple go评论89阅读模式
英文:

How to get an key value pair from an object in angular

问题

  1. sample array:
  2. const obj = {
  3. "29": "DTE Queue",
  4. "30": "Services Reporting Sales",
  5. "31": "Services Reporting Ops",
  6. "41": "UPLOAD",
  7. "55": "Support Report"
  8. };
  9. I'm getting input from user as 'ser'. Then output should be { "30": "Services Reporting Sales", "31": "Services Reporting Ops"}. But the output I'm getting is {"30": "Services Reporting Sales"}.
  10. **Method 1:**
  11. getKeyByValue(obj: object, value) {
  12. const matchedEntry = Object.entries(obj).find(entry =>
  13. entry[1].toLowerCase().match(value.toLowerCase()));
  14. return matchedEntry && (<any>Object).fromEntries([matchedEntry])
  15. }
  16. **Method 2:**
  17. getKeyByValue(obj: Object, value) {
  18. try {
  19. return (<any>Object).fromEntries([
  20. Object.entries(obj).find(([key, val]) =>
  21. val.toLowerCase().startsWith(value.toLowerCase())
  22. ),
  23. ]);
  24. } catch (err) {
  25. console.log("Object not found");
  26. return {};
  27. }
  28. }
英文:

sample array:

  1. const obj = {
  2. &quot;29&quot;: &quot;DTE Queue&quot;,
  3. &quot;30&quot;: &quot;Services Reporting Sales&quot;,
  4. &quot;31&quot;: &quot;Services Reporting Ops&quot;,
  5. &quot;41&quot;: &quot;UPLOAD&quot;,
  6. &quot;55&quot;: &quot;Support Report&quot;
  7. };

I'm getting input from user as 'ser'. Then output should be { "30": "Services Reporting Sales", "31": "Services Reporting Ops"}.But the output I'm getting is {"30": "Services Reporting Sales"}.

Method 1:

  1. getKeyByValue(obj:object, value) {
  2. const matchedEntry = Object.entries(obj).find(entry =&gt;
  3. entry[1].toLowerCase().match(value.toLowerCase()));
  4. return matchedEntry &amp;&amp;(&lt;any&gt;Object).fromEntries([matchedEntry])
  5. }

Method2:

  1. getKeyByValue(obj: Object, value) {
  2. try {
  3. return (&lt;any&gt;Object).fromEntries([
  4. Object.entries(obj).find(([key, val]) =&gt;
  5. val.toLowerCase().startsWith(value.toLowerCase())
  6. ),
  7. ]);
  8. } catch (err) {
  9. console.log(&quot;Object not found&quot;);
  10. return {};
  11. }
  12. }

答案1

得分: 1

你正在使用findfind返回第一个搜索结果。如果你想要多个结果,可以使用filter

  1. const obj = {"29": "DTE Queue", "30": "Services Reporting Sales", "31": "Services Reporting Ops", "41": "UPLOAD", "55": "Support Report"};
  2. function getObjectByValue(object, value) {
  3. return Object.fromEntries(Object.entries(object).filter(([key, val]) => val.toLowerCase().includes(value.toLowerCase())));
  4. }
  5. console.log(getObjectByValue(obj, 'ser'));
  6. console.log(getObjectByValue(obj, 'sup2'));
英文:

You're using find. find returns the first search result. If you want multiple results, you can use filter.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const obj = {&quot;29&quot;: &quot;DTE Queue&quot;, &quot;30&quot;: &quot;Services Reporting Sales&quot;, &quot;31&quot;: &quot;Services Reporting Ops&quot;, &quot;41&quot;: &quot;UPLOAD&quot;, &quot;55&quot;: &quot;Support Report&quot;};
  2. function getObjectByValue(object, value) {
  3. return Object.fromEntries(Object.entries(object).filter(([key, val]) =&gt; val.toLowerCase().includes(value.toLowerCase())));
  4. }
  5. console.log(getObjectByValue(obj, &#39;ser&#39;));
  6. console.log(getObjectByValue(obj, &#39;sup2&#39;));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 15:53:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定