首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图从Javascript中的一系列循环/映射返回<spans>

试图从Javascript中的一系列循环/映射返回<spans>
EN

Stack Overflow用户
提问于 2022-09-05 08:12:35
回答 2查看 28关注 0票数 1

我试图拆分一个输入的字符串,然后遍历一个对象数组,以便将字符串的每个部分与一系列拼写匹配(您可以看到下面的数据结构)。然而,我似乎只是返回undefined

这是我的redux代码

代码语言:javascript
复制
const wordSlice = createSlice({
  name: 'word',
  initialState: { word: '', preppedWord: '' },
  reducers: {
    [...]
    checkPhonemes(state, action) {
      console.log(decoder.phonemeCheck(state.word))
      state.preppedWord = decoder.phonemeCheck(state.word)
    },
  },
})

以下是相关职能:

代码语言:javascript
复制
export const phonemeCheck = (word) => {
  return phonemeMatcher(word)
}

///map((element) => { /* … */ })
export const phonemeMatcher = (word) => {
  for (let i = 0; i < word.length; i++) {
    for (let j = 0; j < word.length - i; j++) {
      let chunk = word.substring(i, i + j + 1)
      consonants.map((consonant) => {
        consonant.spellings.map((spelling) => {
          if (chunk === spelling.spelling) {
            console.log(chunk)
            return <span className={'phoneme-class'}>{chunk}</span>
          }
        })
      })
    }
  }
}

这是数据(节选)

代码语言:javascript
复制
export const consonants = [
  //remember to wrap sounds in / / eg. /s/, /c/
  {
    consonantSound: 's',
    spellings: [
      { spelling: 's', example: 'sun' },
      { spelling: 'ss', example: 'class' },
      { spelling: 'c', example: 'cell' },
      { spelling: 'ce', example: 'voice' },
      { spelling: 'se', example: 'house' },
      { spelling: 'sc', example: 'scent' },
    ],
  },
  {
    consonantSound: 'sh',
    spellings: [
      { spelling: 'sh', example: 'ship' },
      { spelling: 'ch', example: 'machine' },
    ],
  },
  {
    consonantSound: 'ch',
    spellings: [
      { spelling: 'tch', example: 'match' },
      { spelling: 'ch', example: 'chips' },
    ],
  },
[..]
]

这是一把小提琴https://jsfiddle.net/hs3nam5v/9/

EN

回答 2

Stack Overflow用户

发布于 2022-09-05 09:26:18

这样做是有效的:

代码语言:javascript
复制
export const phonemeCheck = (word) => {
  return phonemeMatcher(word)
}

export const phonemeMatcher = (word) => {
  const phonemes = new Set()
  for (let i = 0; i < word.length; i++) {
    for (let j = 0; j < word.length - i; j++) {
      let chunk = word.substring(i, i + j + 1)
      consonants.map((consonant) => {
        consonant.spellings.map((spelling) => {
          console.log(spelling.spelling)
          if (chunk === spelling.spelling) {
            phonemes.add(chunk)
          }
        })
      })
    }
  }

  return Array.from(phonemes).map((phoneme) => {
    return <span className={'phoneme-class'}>/{phoneme}/</span>
  })
}
票数 0
EN

Stack Overflow用户

发布于 2022-09-05 09:29:59

创建一个集并在相同的地图上映射,这样您就不需要使用多个映射。

您可以返回如下内容:

代码语言:javascript
复制
   return Array.from(set).map((element) => {
    return <span className={'phoneme-class'}>/{element}/</span>
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73606367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档