Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从Typescript中的dictionary对象获取匹配值?_Javascript_Node.js_Angular_Typescript_Dictionary - Fatal编程技术网

Javascript 如何从Typescript中的dictionary对象获取匹配值?

Javascript 如何从Typescript中的dictionary对象获取匹配值?,javascript,node.js,angular,typescript,dictionary,Javascript,Node.js,Angular,Typescript,Dictionary,我有一个dictionary对象,其填充方式如下: const myDictionaryElement = this.myDictionary["abc"]; 此处,myDictionaryElement具有以下值: ACheckStatus: "PASS" QVVStatus: "READY" VVQStatus: "READY" QTTStatus: "READY" QBCTTStatus: "READY" CStatus: "FAIL" < P> >我想创建一个对象,使所有的“代码”>

我有一个dictionary对象,其填充方式如下:

const myDictionaryElement = this.myDictionary["abc"];
此处,
myDictionaryElement
具有以下值:

ACheckStatus: "PASS"
QVVStatus: "READY"
VVQStatus: "READY"
QTTStatus: "READY"
QBCTTStatus: "READY"
CStatus: "FAIL"
< P> >我想创建一个对象,使所有的“代码”>键值< /COD>对,中间有匹配的<代码> VV<代码>,应该存储在对象<代码> ValueVV<代码>中:
const valuesVV = { QVVStatus: "READY" };
const valuesTT = { QTTStatus: "READY", QBCTTStatus: "READY" } ;
const valuesOther = { ACheckStatus: "PASS", VVQStatus: "READY", CStatus: "FAIL"  } ;
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;
}
< Similary >所有代码>键值对,其中密钥在匹配中<代码> TT<代码>中间应存储在对象<代码> Valuestt <代码>中:如下:

const valuesVV = { QVVStatus: "READY" };
const valuesTT = { QTTStatus: "READY", QBCTTStatus: "READY" } ;
const valuesOther = { ACheckStatus: "PASS", VVQStatus: "READY", CStatus: "FAIL"  } ;
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;
}

和所有<代码>键值< /代码>对,其中密钥没有匹配<代码> VV 和<代码> TT<代码>中间应存储在对象<代码> ValueSuth/<代码>以下:

const valuesVV = { QVVStatus: "READY" };
const valuesTT = { QTTStatus: "READY", QBCTTStatus: "READY" } ;
const valuesOther = { ACheckStatus: "PASS", VVQStatus: "READY", CStatus: "FAIL"  } ;
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;
}
为了实现about输出,我正在为dictionary使用
hasOwnProperty
,但它不起作用

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

您应该过滤对象关键点并仅选择所需的关键点,然后使用reduce创建新对象:

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

您应该过滤对象关键点并仅选择所需的关键点,然后使用reduce创建新对象:

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

您可以通过创建一个接受dict和matches数组的函数来概括它,如下所示:

const valuesVV = { QVVStatus: "READY" };
const valuesTT = { QTTStatus: "READY", QBCTTStatus: "READY" } ;
const valuesOther = { ACheckStatus: "PASS", VVQStatus: "READY", CStatus: "FAIL"  } ;
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;
}

方法是检查每个键并将其插入正确的bucket(object)

您可以通过创建一个函数来概括它,该函数采用dict和matches数组,如下所示:

const valuesVV = { QVVStatus: "READY" };
const valuesTT = { QTTStatus: "READY", QBCTTStatus: "READY" } ;
const valuesOther = { ACheckStatus: "PASS", VVQStatus: "READY", CStatus: "FAIL"  } ;
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;
}

想法是检查每个键并将其插入正确的bucket(对象)

我认为这将包括
{QVVStatus:“READY”}
VVQStatus:“READY”
但是,我只想要
{QVVStatus:“READY”}
VV
位于middle@meallhour您可以使用
indexOf('VV')>1
,这样它将跳过以
VV
开头的字符串。更新了答案在你的答案中,你有
indexOf('VV')>=1
应该是
indexOf('VV')>1
?它正在工作,我能够获得
valuesv
valuesTT
的值,但是如何在这里包括
非条件
来获得
值,其他类似
的东西!(indexOf('VV')和&!(indexOf('TT')>1)
我认为这将包括
{QVVStatus:“READY”}
VVQStatus:“READY”
但是,我只想要
{QVVStatus:“READY”}
middle@meallhour您可以使用
indexOf('VV'))>1
以跳过以
VV
开头的字符串。更新答案在您的答案中,您的
indexOf('VV')>=1
应该是
indexOf('VV'))>1
?它正在工作,我能够获取
值sv
值stt
的值,但是如何在此处包含
非条件以获取
值其他类似
!(indexOf('VV')和&!(indexOf('TT')>1)