英文:
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 > 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) => item.question.replace(/\s/g, "").toLowerCase() === this.quest.replace(/\s/g, "").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, '').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)
Result:
{ ID: 2, text: 'Where is it?' }
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 => 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=> s == 1))
console.log(`arr has 10`, arr.some(s=> s == 10))
console.log(`arr has 1`, arr.some(s=> s == 6))
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论