简体   繁体   English

如何访问 bool 的名称/键(不是它的值)?

[英]How can I access the name/key of a bool (not its value)?

New to coding and working in JavaScript. JavaScript 编码和工作新手。 I'm sure there's a simple way to do this but it's been hard to pinpoint with Google searches.我确信有一种简单的方法可以做到这一点,但很难通过谷歌搜索来确定。

I'm trying to find a JavaScript function or something of the sort that will find the name of a bool in an array (not the bool's value) and return it to me.我正在尝试找到一个 JavaScript 函数或类似的东西,它会在数组中找到一个 bool 的名称(而不是 bool 的值)并将其返回给我。 Also, the bools have to be in an array.此外,布尔值必须在一个数组中。 I can't make them object keys for my purposes.为了我的目的,我不能让它们成为对象键。

Here is a mock example of what I'm trying to do:这是我正在尝试做的一个模拟示例:

JavaScript: JavaScript:

function dinnerPreferences(){
  var isChinese = false;
  var isItalian = true;
  var isIndian = false;
  var isHomemade = true;
  var isTakeout = false;

  var dinnerOptions = [isChinese, isItalian, isIndian, isHomemade, isTakeout];
  console.log("Dinner options values: " + dinnerOptions);

  function getPreferences(){
    var wantedDinnerOptions = [];
    // Gather the true values in new array: wantedDinnerOptions
    for(i = 0; i <=dinnerOptions.length; i++){

      if(dinnerOptions[i]){
        wantedDinnerOptions.push(dinnerOptions[i]);
      }
    }
    // Access bool values:
    console.log("Confirm true values: " + wantedDinnerOptions);
    // Access bool length:
    console.log("True value index length: " + wantedDinnerOptions.length)
    // Access bool NAMES(???):
    console.log("Object.keys() doesn't work. What else can I use?");
  }
  getPreferences();
}
  dinnerPreferences();

You will have to restructure your dinnerOptions array in someway.您将不得不以某种方式重组您的dinnerOptions数组。 I'd suggest to transform it to an array of objects, as shown below.我建议将其转换为对象数组,如下所示。 So, dinnerOptions is still an array but the items in them are more useable/meaningful when present in form of objects.因此, dinnerOptions仍然是一个数组,但其中的项目以对象形式存在时更有用/更有意义。

 function dinnerPreferences() { const dinnerOptions = [ { type: "Chinese", availability: false }, { type: "Italian", availability: true }, { type: "Indian", availability: false }, { type: "Homemade", availability: true }, { type: "Takeout", availability: false }, ]; console.log( "All dinner options values: " + dinnerOptions.map((opt) => opt.type) ); function getPreferences() { const wantedDinnerOptions = []; for (let i = 0; i < dinnerOptions.length; i++) { if (dinnerOptions[i].availability) { wantedDinnerOptions.push(dinnerOptions[i].type); } } console.log("Available dinner options: " + wantedDinnerOptions); } getPreferences(); } dinnerPreferences();

Also the condition for the for loop was incorrect, i should be strictly less than dinnerOptions.length .此外for循环的条件不正确, i应该严格小于dinnerOptions.length And I'd suggest using let and const instead of var .我建议使用letconst而不是var

 function dinnerPreferences(){ var isChinese = false; var isItalian = true; var isIndian = false; var isHomemade = true; var isTakeout = false; var dinnerOptions = {isChinese:isChinese, isItalian:isItalian, isIndian:isIndian, isHomemade:isHomemade, isTakeout:isTakeout}; console.log("Dinner options values: " + Object.values(dinnerOptions)); function getPreferences(){ var wantedDinnerOptions = []; // Gather the true values in new array: wantedDinnerOptions for(i = 0; i <=dinnerOptions.length; i++){ if(dinnerOptions[i]){ wantedDinnerOptions.push(dinnerOptions[i]); } } // Access bool values: console.log("Confirm true values: " + wantedDinnerOptions); // Access bool length: console.log("True value index length: " + wantedDinnerOptions.length) // Access bool NAMES(???): console.log("Dinner options keys" + Object.keys(dinnerOptions)); } getPreferences(); } dinnerPreferences();

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

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