jeudi 1 juillet 2021

How to include a value from .some in an output?

I'm checking whether words aren't contracted in a sentences array:

const sentences = ['i do not', 'aren\'t you']

const result = sentences.reduce((result, sentence, index, array) => {
  const noncontractions = ['do not', 'are not']
  const isNoncontraction = noncontractions.some(noncontraction => sentence.includes(noncontraction))

  if (isNoncontraction) {
    result.push(sentence)
  }

  return result
}, [])

console.log(result) // list of sentences that aren't contracted.

Right now, the output is ['i do not']. But I also want to include the noncontraction related to the item.

So the output would look like: [{ sentence: 'i do not', noncontraction: 'do not'}]

I'm not sure how to achieve this, since noncontraction is in the .some "loop."

Aucun commentaire:

Enregistrer un commentaire