简体   繁体   English

如何从 Typescript 中的字典 object 中获取匹配值?

[英]How to fetch matching values from a dictionary object in Typescript?

I have a dictionary object which is populated as follows:我有一本字典 object 填充如下:

const myDictionaryElement = this.myDictionary["abc"];

Here, myDictionaryElement has the values:在这里, myDictionaryElement具有以下值:

ACheckStatus: "PASS"
QVVStatus: "READY"
VVQStatus: "READY"
QTTStatus: "READY"
QBCTTStatus: "READY"
CStatus: "FAIL"

I want to create a object such that all key-value pairs for which the key has matching VV in the middle should be stored in object valuesVV as following:我想创建一个 object ,以便键在中间匹配VV的所有key-value对都应存储在 object valuesVV中,如下所示:

const valuesVV = { QVVStatus: "READY" };

Similary all key-value pairs for which the key has matching TT in the middle should be stored in object valuesTT as following:类似地,所有key-value在中间匹配TT的键值对应存储在 object valuesTT中,如下所示:

const valuesTT = { QTTStatus: "READY", QBCTTStatus: "READY" } ;

And all key-value pairs for which the key doesn't have matching VV and TT in the middle should be stored in object valuesOther as following:并且所有key中间没有匹配到VVTTkey-value对都应该存储在object valuesOther中,如下:

const valuesOther = { ACheckStatus: "PASS", VVQStatus: "READY", CStatus: "FAIL"  } ;

To achieve the about output, I am using hasOwnProperty for dictionary but it is not working为了实现关于 output,我使用hasOwnProperty作为字典,但它不起作用

const valuesVV = myDictionaryElement.hasOwnProperty('*VV*');  // It is returning boolean false. The desired output is { QVVStatus: "READY" } 

You should filter the object keys and select only the ones you need, then use reduce to create a new object:你应该只过滤你需要的 object 和 select 键,然后使用 reduce 创建一个新的 object:

 const vvItems = Object.keys(dictElement)
  .filter(key => key.indexOf('VV')>= 1)
  .reduce((o, key) => { o[key] = dictElement[key]; return o; }, {})

You can generalize it by creating a function that takes the dict and a matches array like the following:你可以通过创建一个 function 来概括它,它接受字典和一个匹配数组,如下所示:

const dict = {
  ACheckStatus: "PASS",
  QVVStatus: "READY",
  VVQStatus: "READY",
  QTTStatus: "READY",
  QBCTTStatus: "READY",
  CStatus: "FAIL"
};

const matching = ['VV', 'TT', 'SS'];

function matchInput(input, matches) {
  // will have matched object in the index of the match and those without a match 
  // at the end
  const res = [];

  Object.keys(input).forEach(key => {
    // flag if the key wasn't matched to add it to no matches object
    let matched = false;

    matches.forEach((match, i) => {
      if (key.indexOf(match) > 0) {
        matched = true;
        if (!res[i]) res[i] = {};
        res[i][key] = input[key];
      }
    });

    if (!matched) {
      if (!res[matches.length]) res[matches.length] = {};
      res[matches.length][key] = input[key];
    }
  });

  return res;
}

The idea is to go over each key and insert it in the right bucket (object)这个想法是在每个键上 go 并将其插入到正确的存储桶(对象)中

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

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