避免JavaScript中的重复方式

huangapple go评论115阅读模式
英文:

Avoid duplication in JavaScript way

问题

我想防止重复添加数值。

如果(this.sQuestions.findIndex((item)=>
item.question.replace(/\s/g,"").toLowerCase()===
this.quest.replace(/\s/g,"").toLowerCase())>0)
{
this.isQuestionExist = true;
}
否则 {
//保存功能
}

它有效,除了sQuestions[0]元素,为什么?

英文:

I would like to prevent for adding duplicate values.

  1. if (this.sQuestions.findIndex((item) =>
  2. item.question.replace(/\s/g, "").toLowerCase() ===
  3. this.quest.replace(/\s/g, "").toLowerCase()) > 0)
  4. {
  5. this.isQuestionExist = true;
  6. }
  7. else {
  8. //save function
  9. }

It's working except the sQuestions[0] element, Why?.

答案1

得分: 3

建议使用Set来实现此目的。正如文档所述:

Set对象允许您存储任何类型的唯一值,无论是原始值还是对象引用。

文档中的示例:

  1. const set1 = new Set([1, 2, 3, 4, 5]);
  2. console.log(set1.has(1));
  3. // 期望输出:true
  4. console.log(set1.has(5));
  5. // 期望输出:true
  6. console.log(set1.has(6));
  7. // 期望输出:false

希望这能帮助您!

英文:

I suggest to use Set for this purpose. As the documentation states:

> The Set object lets you store unique values of any type, whether primitive values or object references.

Example from the documentation:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const set1 = new Set([1, 2, 3, 4, 5]);
  2. console.log(set1.has(1));
  3. // expected output: true
  4. console.log(set1.has(5));
  5. // expected output: true
  6. console.log(set1.has(6));
  7. // expected output: false

<!-- end snippet -->

I hope this helps!

答案2

得分: 1

你正在比较是否大于 0,如果数组中找不到该项,函数将返回 -1,因此应该与 -1 进行比较。

这将会起作用。

  1. if (this.sQuestions.findIndex((item) => item.question.replace(/\s/g, "").toLowerCase() === this.quest.replace(/\s/g, "").toLowerCase()) !== -1) {
  2. this.isQuestionExist = true;
  3. }
  4. else {
  5. // 保存函数
  6. }
英文:

You are comparing against &gt; 0 if the item is not found in the array, the function will return -1 so it should be compared against -1

This will be working.

  1. if (this.sQuestions.findIndex((item) =&gt; item.question.replace(/\s/g, &quot;&quot;).toLowerCase() === this.quest.replace(/\s/g, &quot;&quot;).toLowerCase()) !== -1) {
  2. this.isQuestionExist = true;
  3. }
  4. else {
  5. //save function
  6. }

答案3

得分: 1

根据您的问题,您可以尝试

  1. let uniqueQuestions = new Set(this.sQuestions)
英文:

According to your question, you can try

  1. let uniqueQuestions = new Set(this.sQuestions)

答案4

得分: 1

为了性能和清晰度的原因,您应该将问题索引在一个专用的结构中,比如Map

  1. function getKey(question)
  2. {
  3. return question.replace(/\s/g, '').toLowerCase()
  4. }
  5. var question1 = {
  6. ID: 1,
  7. text: 'What time is it?'
  8. }
  9. var question2 = {
  10. ID: 2,
  11. text: 'Where is it?'
  12. }
  13. var question3 = {
  14. ID: 3,
  15. text: 'What is it?'
  16. }
  17. var questions = new Map()
  18. questions.set(getKey(question1.text), question1)
  19. questions.set(getKey(question2.text), question2)
  20. questions.set(getKey(question3.text), question3)
  21. var quest = 'Where is it?'
  22. var match = questions.get(getKey(quest))
  23. console.log(match)
  24. quest = 'WTF?'
  25. match = questions.get(getKey(quest))
  26. console.log(match)

结果:

  1. { ID: 2, text: 'Where is it?' }
  2. undefined
英文:

For performance and clarity reasons you should index your questions in a dedicated structure like a Map:

  1. function getKey(question)
  2. {
  3. return question.replace(/\s/g, &#39;&#39;).toLowerCase()
  4. }
  5. var question1 = {
  6. ID: 1,
  7. text: &#39;What time is it?&#39;
  8. }
  9. var question2 = {
  10. ID: 2,
  11. text: &#39;Where is it?&#39;
  12. }
  13. var question3 = {
  14. ID: 3,
  15. text: &#39;What is it?&#39;
  16. }
  17. var questions = new Map()
  18. questions.set(getKey(question1.text), question1)
  19. questions.set(getKey(question2.text), question2)
  20. questions.set(getKey(question3.text), question3)
  21. var quest = &#39;Where is it?&#39;
  22. var match = questions.get(getKey(quest))
  23. console.log(match)
  24. quest = &#39;WTF?&#39;
  25. match = questions.get(getKey(quest))
  26. console.log(match)

Result:

  1. { ID: 2, text: &#39;Where is it?&#39; }
  2. undefined

答案5

得分: 1

你可以使用 some 方法来检查数组中是否存在数据:

  1. if (!this.sQuestions.some(q => q.question == newQuestion))
  2. {
  3. this.isQuestionExist = true;
  4. }
  5. else {
  6. // 保存函数
  7. }

一个示例:

  1. let arr = [1, 2, 3, 4, 5];
  2. console.log(`arr has 1`, arr.some(s => s == 1))
  3. console.log(`arr has 10`, arr.some(s => s == 10))
  4. console.log(`arr has 1`, arr.some(s => s == 6))
英文:

You can use some method to check whether the data is existing in your array:

  1. if (!this.sQuestions.some(q =&gt; q.question == newQuestion))
  2. {
  3. this.isQuestionExist = true;
  4. }
  5. else {
  6. //save function
  7. }

An example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. let arr = [1, 2, 3, 4, 5];
  2. console.log(`arr has 1`, arr.some(s=&gt; s == 1))
  3. console.log(`arr has 10`, arr.some(s=&gt; s == 10))
  4. console.log(`arr has 1`, arr.some(s=&gt; s == 6))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 20:50:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定