[英]How to iterate through two array and compare their elements
I want to compare each element of two arrays.我想比较两个 arrays 的每个元素。 Person writes down something in input.
人在输入中写下一些东西。 I want to compare what person had written with another phrase.
我想将某人写的内容与另一个短语进行比较。 The idea I took from this site https://lingua.com/german/dictation/ , you can check to understand what i want.
我从这个网站https://lingua.com/german/dictation/获得的想法,您可以查看以了解我想要的内容。 It is kind of dictation, person writes down something and at the end he can check mistakes and so on.
这是一种听写,人们写下一些东西,最后他可以检查错误等等。 I tried to realize function with two different ways, but it does not work
我尝试用两种不同的方式实现function,但它不起作用
let [value, setValue] = useState("currentValue")
let [correctText, setCorrectText] = useState("Every day I work hard to achive my goals")
let [answer, setAnswer] = useState<string[]>([])
// first variant
let compare = () => {
let correctTextValue = correctText.split(" ")
let valueArray = value.split(" ")
let arr = []
for (let i=0; i<correctTextValue.length; i++) {
for (let j=0; j<valueArray.length; j++) {
if (correctTextValue[i]===valueArray[j]) {
arr.push(valueArray[j])
}
else {
arr.push(correctTextValue[j]
}
}
}
setAnswer([...answer, ...arr])
}
//second variant
let compare = () => {
let correctTextValue = correctText.split(" ")
let valueArray = value.split(" ")
let arr = []
for (let i=0; i<correctTextValue.length; i++) {
for (let j=0; j<valueArray.length; j++) {
if (correctTextValue[i]===valueArray[j]) {
setAnswer([...answer, valueArray[j]])
}
else {
setAnswer([...answer, correctTextValue[j]])
}
}
}
}
// this is simple jsx code
<input value={value} onChange={e=>setValue(e.target.value)}/>
<button onClick={() => compare()}>Submit</button>
<div>{answer.map(el=><span> {el} </span>)}</div>
t.fail = () => {throw Error("Unit test failed.")};
t.seqsUnequal = (a, b) => {
/* if (a === b) return false; */
let value, done;
const b_iter = b[Symbol.iterator]();
for (const a_elt of a) {
({value, done} = b_iter.next());
if (done) return "b short";
if (value !== a_elt)
return `different values ${typeof value}:${typeof a_elt}`;
};
({value, done} = b_iter.next());
if (! done) return "a short";
return false
};
t.seqsEqual = (a, b) => ! t.seqsUnequal(a, b);
t.exp_seq_0 = ["Greens"];
if (! t.seqsEqual(t.exp_seq_0, t.exp_seq_0)) t.fail();
if (! t.seqsEqual([], [])) t.fail();
if (t.seqsEqual(["foo"], [])) t.fail();
if (t.seqsEqual([], [0])) t.fail();
if (! t.seqsEqual(["fox", "went", "out", "on", "a", "winter's", "night"]
, ["fox", "went", "out", "on", "a", "winter's", "night"])) t.fail();
The above code will shallow-compare any two iterables for equality.上面的代码将浅比较任何两个可迭代的相等性。 But given that you know that you have arrays, you can use indexing, for a much simpler solution.
但鉴于您知道您有 arrays,您可以使用索引,以获得更简单的解决方案。
Here's an updated version of your code that should work (if I understood correctly).这是应该可以工作的代码的更新版本(如果我理解正确的话)。 It compares the words from
correctText
and value
word by word and marks different words as isCorrect: false
.它逐字比较来自
correctText
和value
的单词,并将不同的单词标记为isCorrect: false
。 It then uses that information inside the JSX to add underline styles to incorrect words.然后,它使用 JSX 中的信息在不正确的单词中添加下划线 styles。
let [value, setValue] = useState("")
let [correctText, setCorrectText] = useState("Every day I work hard to achive my goals")
let [answer, setAnswer] = useState([])
let compare = () => {
let correctTextValue = correctText.split(" ")
let valueArray = value.split(" ")
let arr = correctTextValue.map((word, index) => {
if (word === valueArray[i]) {
return {
isCorrect: true,
word: word
};
}
return {
isCorrect: false,
word: valueArray[i]
};
});
setAnswer(arr);
}
<input value={value} onChange={e=>setValue(e.target.value)}/>
<button onClick={() => compare()}>Submit</button>
<div>{
answer.map(el => {
return el.isCorrect
? <span>el.word</span>
: <span style="text-decoration:underline">el.word</span>
})
}</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.