避免JavaScript中的重复方式

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

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.

if (this.sQuestions.findIndex((item) => 
    item.question.replace(/\s/g, "").toLowerCase() ===  
        this.quest.replace(/\s/g, "").toLowerCase()) > 0) 
{
  this.isQuestionExist = true;
}
else {
  //save function
}

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

答案1

得分: 3

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

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

文档中的示例:

const set1 = new Set([1, 2, 3, 4, 5]);

console.log(set1.has(1));
// 期望输出:true

console.log(set1.has(5));
// 期望输出:true

console.log(set1.has(6));
// 期望输出: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 -->

const set1 = new Set([1, 2, 3, 4, 5]);

console.log(set1.has(1));
// expected output: true

console.log(set1.has(5));
// expected output: true

console.log(set1.has(6));
// expected output: false

<!-- end snippet -->

I hope this helps!

答案2

得分: 1

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

这将会起作用。

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

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.

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

答案3

得分: 1

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

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

According to your question, you can try

let uniqueQuestions = new Set(this.sQuestions)

答案4

得分: 1

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

function getKey(question)
{
    return question.replace(/\s/g, '').toLowerCase()
}

var question1 = {
    ID: 1,
    text: 'What time is it?'
}

var question2 = {
    ID: 2,
    text: 'Where is it?'
}

var question3 = {
    ID: 3,
    text: 'What is it?'
}

var questions = new Map()
questions.set(getKey(question1.text), question1)
questions.set(getKey(question2.text), question2)
questions.set(getKey(question3.text), question3)

var quest = 'Where is it?'
var match = questions.get(getKey(quest))
console.log(match)

quest = 'WTF?'
match = questions.get(getKey(quest))
console.log(match)

结果:

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

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

function getKey(question)
{
	return question.replace(/\s/g, &#39;&#39;).toLowerCase()
}

var question1 = {
	ID: 1,
	text: &#39;What time is it?&#39;
}

var question2 = {
	ID: 2,
	text: &#39;Where is it?&#39;
}

var question3 = {
	ID: 3,
	text: &#39;What is it?&#39;
}

var questions = new Map()
questions.set(getKey(question1.text), question1)
questions.set(getKey(question2.text), question2)
questions.set(getKey(question3.text), question3)

var quest = &#39;Where is it?&#39;
var match = questions.get(getKey(quest))
console.log(match)

quest = &#39;WTF?&#39;
match = questions.get(getKey(quest))
console.log(match)

Result:

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

答案5

得分: 1

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

if (!this.sQuestions.some(q => q.question == newQuestion)) 
{
  this.isQuestionExist = true;
}
else {
  // 保存函数
}

一个示例:

let arr = [1, 2, 3, 4, 5];
console.log(`arr has 1`, arr.some(s => s == 1))
console.log(`arr has 10`, arr.some(s => s == 10))
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:

if (!this.sQuestions.some(q =&gt; q.question == newQuestion)) 
{
  this.isQuestionExist = true;
}
else {
  //save function
}

An example:

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

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

let arr = [1, 2, 3, 4, 5];
console.log(`arr has 1`, arr.some(s=&gt; s == 1))
console.log(`arr has 10`, arr.some(s=&gt; s == 10))
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:

确定