简体   繁体   English

分配嵌套的 map 唯一键,javascrpit

[英]Assign Nested map unique key, javascrpit

I need to assign assign each input their own id.我需要为每个输入分配自己的 ID。

const sep = words.map((word, ind) => {
  return (
     <div key={ind} className="guess__word">
       {word.split("").map((letter, id) => {
         if (letter === " ") {
           return <SpaceBox key={id} letter={letter} />;
         } else {
           return <LetterBox key={id} id={id} letter={letter} />;
         }
      })}
     </div>
   );
 });

The problem that I am having is that due to the map being nested, the value of id keeps reseting back to 0. Each word of words has a different length so if kinda hard to figure out if there is a math equation that would help me solve this.我遇到的问题是,由于 map 被嵌套,id 的值不断重置为 0。单词的每个单词都有不同的长度,所以很难弄清楚是否有一个数学方程式可以帮助我解决这个。

for reference, the length of each word is 10, 3, 4, and 4.作为参考,每个单词的长度为 10、3、4 和 4。

Also I need the ids to equal up to the length of words.我还需要 id 等于单词的长度。 So I would need the ids to be 0-20.所以我需要 ids 为 0-20。

if kinda hard to figure out if there is a math equation that would help me solve this.如果有点难以弄清楚是否有一个数学方程式可以帮助我解决这个问题。

There is - given indicies X and Y, you can map onto the Xth prime number raised to the Y power - but that's way overkill here.有 - 给定指标 X 和 Y,您可以将 map 加到第 X 个素数的 Y 次方 - 但这里太过分了。 Just use string concatenation, for the key, eg indicies 3 and 4 can produce a key of 3_4 .只需使用字符串连接,对于键,例如索引 3 和 4 可以产生键3_4

I'd recommend not using .split('') to get a character array from a string - invoke the array's iterator instead, otherwise you'll occasionally run into problems when odd characters are present.我建议不要使用.split('')从字符串中获取字符数组 - 而是调用数组的迭代器,否则当出现奇数字符时偶尔会遇到问题。

const sep = words.map((word, ind) => {
  return (
     <div key={ind} className="guess__word">
       {[...word].map((letter, id) => {
         const key = `${ind}_${id}`;
         if (letter === " ") {
           return <SpaceBox key={key} letter={letter} />;
         } else {
           return <LetterBox key={key} id={id} letter={letter} />;
         }
      })}
     </div>
   );
 });

If the words may change, you might want to use a slightly different key, to differentiate a SpaceBox letter from a LetterBox letter.如果单词可能会发生变化,您可能希望使用稍微不同的键来区分SpaceBox字母和LetterBox字母。

<LetterBox key={key + ' '} id={id} letter={letter} />

If you have to use only array indicies in order starting from 0, it'll be uglier - declare the last used number outside, and use and increment it inside the loop.如果你必须只使用数组索引从 0 开始,它会更难看 - 在外面声明最后使用的数字,并在循环内使用和递增它。

let lastId = -1;
const sep = words.map((word) => {
  return (
     <div key={ind} className="guess__word">
       {[...word].map((letter) => {
         if (letter === " ") {
           return <SpaceBox key={++lastId} letter={letter} />;
         } else {
           return <LetterBox key={++lastId} id={lastId} letter={letter} />;
         }
      })}
     </div>
   );
 });

Due to the odd key requirement, the above is only a good approach if the words are static. If they may change, memoize the boxes based on the words .由于奇数键的要求,以上只是一个好的方法,如果单词是 static。如果它们可能会改变,根据words memoize boxes

To save math, you might as well just concatenate the numbers in a meaningful way为了节省数学,您不妨以有意义的方式连接数字

const sep = words.map((word, ind) => {
  return (
     <div key={ind} className="guess__word">
       {word.split("").map((letter, id) => {
         const key = `${ind}-{key}`;
         if (letter === " ") {
           return <SpaceBox key={key} letter={letter} />;
         } else {
           return <LetterBox key={key} id={id} letter={letter} />;
         }
      })}
     </div>
   );
 });

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

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