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

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

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

问题

sample array:

const obj = {
  "29": "DTE Queue",
  "30": "Services Reporting Sales",
  "31": "Services Reporting Ops",
  "41": "UPLOAD",
  "55": "Support Report"
};

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:**

getKeyByValue(obj: object, value) {
  const matchedEntry = Object.entries(obj).find(entry =>
  entry[1].toLowerCase().match(value.toLowerCase()));
  return matchedEntry && (<any>Object).fromEntries([matchedEntry])
}

**Method 2:**

getKeyByValue(obj: Object, value) {
  try {
    return (<any>Object).fromEntries([
      Object.entries(obj).find(([key, val]) =>
        val.toLowerCase().startsWith(value.toLowerCase())
      ),
    ]);
  } catch (err) {
    console.log("Object not found");
    return {};
  }
}
英文:

sample array:

  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;
  };

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:

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

Method2:

getKeyByValue(obj: Object, value) {
    try {
      return (&lt;any&gt;Object).fromEntries([
        Object.entries(obj).find(([key, val]) =&gt;
          val.toLowerCase().startsWith(value.toLowerCase())
        ),
      ]);
    } catch (err) {
      console.log(&quot;Object not found&quot;);
      return {};
    }
  }

答案1

得分: 1

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

const obj = {"29": "DTE Queue", "30": "Services Reporting Sales", "31": "Services Reporting Ops", "41": "UPLOAD", "55": "Support Report"};

function getObjectByValue(object, value) {
    return Object.fromEntries(Object.entries(object).filter(([key, val]) => val.toLowerCase().includes(value.toLowerCase())));
}

console.log(getObjectByValue(obj, 'ser'));
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 -->

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;};

function getObjectByValue(object, value) {
    return Object.fromEntries(Object.entries(object).filter(([key, val]) =&gt; val.toLowerCase().includes(value.toLowerCase())));
}

console.log(getObjectByValue(obj, &#39;ser&#39;));
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:

确定