简体   繁体   English

检查数组的第一个字母并设置对象的属性(如果存在)

[英]Check first letter of array and set property on an object if exists

I have an array of site tenants, this looks something like 我有一系列的站点租户,这看起来像

['foo','bar','boo','baz']

I also have an object which controls the state of filter. 我还有一个控制过滤器状态的对象。

This filter is used to control which tenants are visible, this looks something like 此过滤器用于控制可见的租户,这看起来像

[
  { text: 'ALL', disbaled: false },
  { text: 'A', disbaled: true },
  { text: 'B', disbaled: true },
  { text: 'C', disbaled: true },
  { text: 'D', disbaled: true },
  { text: 'E', disbaled: true },
  { text: 'F', disbaled: true },
  { text: 'G', disbaled: true },
  { text: 'H', disbaled: true },
  { text: 'I', disbaled: true },
  { text: 'J', disbaled: true },
  { text: 'K', disbaled: true },
  { text: 'L', disbaled: true },
  { text: 'M', disbaled: true },
  { text: 'N', disbaled: true },
  { text: 'O', disbaled: true },
  { text: 'P', disbaled: true },
  { text: 'Q', disbaled: true },
  { text: 'R', disbaled: true },
  { text: 'S', disbaled: true },
  { text: 'T', disbaled: true },
  { text: 'U', disbaled: true },
  { text: 'V', disbaled: true },
  { text: 'W', disbaled: true },
  { text: 'X', disbaled: true },
  { text: 'Y', disbaled: true },
  { text: 'Z', disbaled: true },
];

What I would like to do is loop over this tenant list and for each string I find, set the corresponding starting letter's disabled flag to false in my letter object 我想做的是遍历此租户列表,并为找到的每个字符串在我的字母对象中将相应的起始字母的disabled标志设置为false

I have a filter elsewhere that is allowing me to control which tenants are displyed, like so... 我在其他地方有一个过滤器,可以让我控制要分配的房客,就像这样...

const byFirstLetter = letter => {
  if (letter === 'ALL') return () => true;

  const rgx = new RegExp(`^${letter}`);
  return tenant => rgx.test(tenant);
};

tenants.filter(byFirstLetter(selector)).map(column => (
  <div className="column is-half">
    <ul>{column.map(client => <li key={client}>{client}</li>)}</ul>
  </div>
))}

I am struiggling to work out how I can do this though, I was thinking of looping over, creating a new object then using object.assign to replace the updated values, but am not sure if this is correct and cannot make it work. 我正在努力研究如何做到这一点,当时我正在考虑进行循环,创建一个新对象,然后使用object.assign替换更新的值,但是不确定这是否正确并且无法使其正常工作。

There's some ambiguity in your question (especially around All ), but I think this is the right direction for you. 您的问题中存在一些歧义(尤其是在All周围),但是我认为这是您的正确方向。

const tenants = ['foo','bar','boo','baz'];

let letters = [
  { text: 'ALL', disabled: false },
  { text: 'A', disabled: true },
  { text: 'B', disabled: true },
  { text: 'C', disabled: true },
  { text: 'D', disabled: true },
  { text: 'E', disabled: true },
  { text: 'F', disabled: true },
  { text: 'G', disabled: true },
  { text: 'H', disabled: true },
  { text: 'I', disabled: true },
  { text: 'J', disabled: true },
  { text: 'K', disabled: true },
  { text: 'L', disabled: true },
  { text: 'M', disabled: true },
  { text: 'N', disabled: true },
  { text: 'O', disabled: true },
  { text: 'P', disabled: true },
  { text: 'Q', disabled: true },
  { text: 'R', disabled: true },
  { text: 'S', disabled: true },
  { text: 'T', disabled: true },
  { text: 'U', disabled: true },
  { text: 'V', disabled: true },
  { text: 'W', disabled: true },
  { text: 'X', disabled: true },
  { text: 'Y', disabled: true },
  { text: 'Z', disabled: true },
];

const disableLetters = (all) => {
  if (all) {
    // ... I'm not sure what your intentions for the "All" index is supposed to be
    // but handle it here.
    return;
  }

  tenants.forEach(tenant => {
    const firstLetter = tenant.length ? tentant[0] : '';

    letters = letters.map(letter => {
      letter.disabled = letter.text.toLowerCase() !== firstLetter.toLowerCase();

      return letter;
    });
  });
}

// Usage:

disableLetters();

Maybe this is what you are trying to do. 也许这就是您想要做的。

Given an array of names : 给定一个名称数组:

var tenant = ['ambr','raul','marcio','gustavo','sandra']

Set to false the disabled property in the letter_arr array where the text is equal to the starting char of the name. letter_arr数组中的disabled属性设置为false,其中text等于名称的起始字符。

This can be done with the following function: 可以使用以下功能完成此操作:

 tenant.map(a=>letter_arr.filter(b=>b.text.toLowerCase()==a[0].toLowerCase()).map(b=>b.disabled=false))

Explanation 说明

tenant is your array of names tenant是您的姓名数组

map is used to loop over the tenant array map用于遍历tenant数组

Then we filter the letter_arr by the first char of the tenant. 然后,我们通过租户的第一个字符过滤letter_arr

Then, for each result, we map again and set the disabled property to false. 然后,对于每个结果,我们再次进行map ,并将disabled属性设置为false。

 var letter_arr = [ { text: 'ALL', disbaled: false }, { text: 'A', disbaled: true }, { text: 'B', disbaled: true }, { text: 'C', disbaled: true }, { text: 'D', disbaled: true }, { text: 'E', disbaled: true }, { text: 'F', disbaled: true }, { text: 'G', disbaled: true }, { text: 'H', disbaled: true }, { text: 'I', disbaled: true }, { text: 'J', disbaled: true }, { text: 'K', disbaled: true }, { text: 'L', disbaled: true }, { text: 'M', disbaled: true }, { text: 'N', disbaled: true }, { text: 'O', disbaled: true }, { text: 'P', disbaled: true }, { text: 'Q', disbaled: true }, { text: 'R', disbaled: true }, { text: 'S', disbaled: true }, { text: 'T', disbaled: true }, { text: 'U', disbaled: true }, { text: 'V', disbaled: true }, { text: 'W', disbaled: true }, { text: 'X', disbaled: true }, { text: 'Y', disbaled: true }, { text: 'Z', disbaled: true }, ]; var tenant = ['ambr','raul','marcio','gustavo','sandra'] tenant.map(a=>letter_arr.filter(b=>b.text.toLowerCase()==a[0].toLowerCase()).map(b=>b.disbaled=false)) console.log(letter_arr) 

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

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