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

时间:2018-03-21 17:30:21

标签: javascript arrays loops

我有一系列网站租户,看起来像

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

我还有一个控制过滤器状态的对象。

此过滤器用于控制哪些租户可见,这看起来像

[
  { 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 },
];

我想要做的是遍历此租户列表,对于我找到的每个字符串,在我的字母对象中将相应的起始字母的禁用标志设置为false

我在其他地方有一个过滤器,允许我控制哪些租户是显示的,就像这样......

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

我正在努力研究如何做到这一点,我正在考虑循环,创建一个新对象,然后使用object.assign替换更新的值,但我不确定这是否正确,无法做到工作

2 个答案:

答案 0 :(得分:0)

你的问题有些含糊不清(特别是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();

答案 1 :(得分:0)

也许这就是你想要做的事情。

给出一系列名称:

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

disabled数组中的letter_arr属性设置为false,其中text等于名称的起始字符。

可以使用以下功能完成此操作:

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

说明

tenant是您的名字数组

map用于遍历tenant数组

然后我们按租户的第一个字符过滤letter_arr

然后,对于每个结果,我们再次map并将disabled属性设置为false。

&#13;
&#13;
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)
&#13;
&#13;
&#13;