How can I check whether an item already exists in state?











up vote
2
down vote

favorite












Is there anyway that I could check if a number already exists (and for instance, if there are more than one instance of a particular number, etc) in state with this code? I can´t figure out any, and I´ve tried all methods I could think of (Indexof, contains, etc, even turning value into an array didn´t help).



const button1 = [{number: 'one', value: '1'},{number: 'two', value: '2'},{number: 'three', value: '3'},{number: 'divide', value: '/'}];

const button2 = [{number: 'four', value: '4'},{number: 'five', value: '5'},{number: 'six', value: '6'},{number: 'add', value: '+'}];

const button3 = [{number: 'seven', value: '7'},{number: 'eight', value: '8'},{number: 'nine', value: '9'},{number: 'subtract', value: '-'}];

const button4 = [{number: 'zero', value: '0'},{number: 'decimal', value: '.'},{number: 'equals', value: '='},{number: 'multiply', value: '*'}];


class Calculator extends Component {
constructor(props) {
super(props);
this.state = { value: "0"};
this.handleClick = this.handleClick.bind(this);
}


handleClick(evt) {
const id = evt.target.id;
const result = evt.target.value;

switch(id) {
case 'clear':
this.setState({ value: "0"});
break;
case 'equals':
this.setState(prevState => ({
value: math.eval(this.state.value)
}));
break;

default: this.setState(prevState => ({
value: `${prevState.value}${result}`.replace(/([/+-/*=])([/+-*=])/g, "$2")
.replace(/^0+(?=[1-9])/, "")
.replace(/^0+(?=.)/, "0")
.replace(/^0+B/, "")
.replace(/.+/g,".")
}));
}


}







render() {
return(
<div id="container">
<Display value={this.state.value} />
<div>
{button1.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}

</div>
<div>
{button2.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
</div>
<div>
{button3.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
</div>
<div>
{button4.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
</div>
<div>
<Button onClick={this.handleClick} id="clear" value={'clear'} />
</div>
</div>
)

}

}









share|improve this question
























  • Check out here: regex101.com/r/fFIS01/1
    – Masious
    Nov 14 at 2:23












  • That regex doesn´t seem to match anything. I have a small problem with the decimal input (I need to block out certain contexts: number.number.number for instance or number +.). My previous methods didn´t seem to accomplish this, so I want to see if I could check for the number in state before adding a decimal.
    – Hernan Ariel
    Nov 14 at 2:26










  • Can you provide a jsFiddle?
    – GMaiolo
    Nov 14 at 2:40










  • Yeah. Basically this, but with the buttons mapped from arrays: codesandbox.io/s/z293rk7y2m
    – Hernan Ariel
    Nov 14 at 2:53










  • @HernanAriel - number +. is a valid context right? it should be treated as number + 0.number ?
    – Ramesh
    Nov 14 at 3:35















up vote
2
down vote

favorite












Is there anyway that I could check if a number already exists (and for instance, if there are more than one instance of a particular number, etc) in state with this code? I can´t figure out any, and I´ve tried all methods I could think of (Indexof, contains, etc, even turning value into an array didn´t help).



const button1 = [{number: 'one', value: '1'},{number: 'two', value: '2'},{number: 'three', value: '3'},{number: 'divide', value: '/'}];

const button2 = [{number: 'four', value: '4'},{number: 'five', value: '5'},{number: 'six', value: '6'},{number: 'add', value: '+'}];

const button3 = [{number: 'seven', value: '7'},{number: 'eight', value: '8'},{number: 'nine', value: '9'},{number: 'subtract', value: '-'}];

const button4 = [{number: 'zero', value: '0'},{number: 'decimal', value: '.'},{number: 'equals', value: '='},{number: 'multiply', value: '*'}];


class Calculator extends Component {
constructor(props) {
super(props);
this.state = { value: "0"};
this.handleClick = this.handleClick.bind(this);
}


handleClick(evt) {
const id = evt.target.id;
const result = evt.target.value;

switch(id) {
case 'clear':
this.setState({ value: "0"});
break;
case 'equals':
this.setState(prevState => ({
value: math.eval(this.state.value)
}));
break;

default: this.setState(prevState => ({
value: `${prevState.value}${result}`.replace(/([/+-/*=])([/+-*=])/g, "$2")
.replace(/^0+(?=[1-9])/, "")
.replace(/^0+(?=.)/, "0")
.replace(/^0+B/, "")
.replace(/.+/g,".")
}));
}


}







render() {
return(
<div id="container">
<Display value={this.state.value} />
<div>
{button1.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}

</div>
<div>
{button2.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
</div>
<div>
{button3.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
</div>
<div>
{button4.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
</div>
<div>
<Button onClick={this.handleClick} id="clear" value={'clear'} />
</div>
</div>
)

}

}









share|improve this question
























  • Check out here: regex101.com/r/fFIS01/1
    – Masious
    Nov 14 at 2:23












  • That regex doesn´t seem to match anything. I have a small problem with the decimal input (I need to block out certain contexts: number.number.number for instance or number +.). My previous methods didn´t seem to accomplish this, so I want to see if I could check for the number in state before adding a decimal.
    – Hernan Ariel
    Nov 14 at 2:26










  • Can you provide a jsFiddle?
    – GMaiolo
    Nov 14 at 2:40










  • Yeah. Basically this, but with the buttons mapped from arrays: codesandbox.io/s/z293rk7y2m
    – Hernan Ariel
    Nov 14 at 2:53










  • @HernanAriel - number +. is a valid context right? it should be treated as number + 0.number ?
    – Ramesh
    Nov 14 at 3:35













up vote
2
down vote

favorite









up vote
2
down vote

favorite











Is there anyway that I could check if a number already exists (and for instance, if there are more than one instance of a particular number, etc) in state with this code? I can´t figure out any, and I´ve tried all methods I could think of (Indexof, contains, etc, even turning value into an array didn´t help).



const button1 = [{number: 'one', value: '1'},{number: 'two', value: '2'},{number: 'three', value: '3'},{number: 'divide', value: '/'}];

const button2 = [{number: 'four', value: '4'},{number: 'five', value: '5'},{number: 'six', value: '6'},{number: 'add', value: '+'}];

const button3 = [{number: 'seven', value: '7'},{number: 'eight', value: '8'},{number: 'nine', value: '9'},{number: 'subtract', value: '-'}];

const button4 = [{number: 'zero', value: '0'},{number: 'decimal', value: '.'},{number: 'equals', value: '='},{number: 'multiply', value: '*'}];


class Calculator extends Component {
constructor(props) {
super(props);
this.state = { value: "0"};
this.handleClick = this.handleClick.bind(this);
}


handleClick(evt) {
const id = evt.target.id;
const result = evt.target.value;

switch(id) {
case 'clear':
this.setState({ value: "0"});
break;
case 'equals':
this.setState(prevState => ({
value: math.eval(this.state.value)
}));
break;

default: this.setState(prevState => ({
value: `${prevState.value}${result}`.replace(/([/+-/*=])([/+-*=])/g, "$2")
.replace(/^0+(?=[1-9])/, "")
.replace(/^0+(?=.)/, "0")
.replace(/^0+B/, "")
.replace(/.+/g,".")
}));
}


}







render() {
return(
<div id="container">
<Display value={this.state.value} />
<div>
{button1.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}

</div>
<div>
{button2.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
</div>
<div>
{button3.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
</div>
<div>
{button4.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
</div>
<div>
<Button onClick={this.handleClick} id="clear" value={'clear'} />
</div>
</div>
)

}

}









share|improve this question















Is there anyway that I could check if a number already exists (and for instance, if there are more than one instance of a particular number, etc) in state with this code? I can´t figure out any, and I´ve tried all methods I could think of (Indexof, contains, etc, even turning value into an array didn´t help).



const button1 = [{number: 'one', value: '1'},{number: 'two', value: '2'},{number: 'three', value: '3'},{number: 'divide', value: '/'}];

const button2 = [{number: 'four', value: '4'},{number: 'five', value: '5'},{number: 'six', value: '6'},{number: 'add', value: '+'}];

const button3 = [{number: 'seven', value: '7'},{number: 'eight', value: '8'},{number: 'nine', value: '9'},{number: 'subtract', value: '-'}];

const button4 = [{number: 'zero', value: '0'},{number: 'decimal', value: '.'},{number: 'equals', value: '='},{number: 'multiply', value: '*'}];


class Calculator extends Component {
constructor(props) {
super(props);
this.state = { value: "0"};
this.handleClick = this.handleClick.bind(this);
}


handleClick(evt) {
const id = evt.target.id;
const result = evt.target.value;

switch(id) {
case 'clear':
this.setState({ value: "0"});
break;
case 'equals':
this.setState(prevState => ({
value: math.eval(this.state.value)
}));
break;

default: this.setState(prevState => ({
value: `${prevState.value}${result}`.replace(/([/+-/*=])([/+-*=])/g, "$2")
.replace(/^0+(?=[1-9])/, "")
.replace(/^0+(?=.)/, "0")
.replace(/^0+B/, "")
.replace(/.+/g,".")
}));
}


}







render() {
return(
<div id="container">
<Display value={this.state.value} />
<div>
{button1.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}

</div>
<div>
{button2.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
</div>
<div>
{button3.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
</div>
<div>
{button4.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
</div>
<div>
<Button onClick={this.handleClick} id="clear" value={'clear'} />
</div>
</div>
)

}

}






javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 at 3:08









isherwood

36.4k1080111




36.4k1080111










asked Nov 14 at 2:16









Hernan Ariel

13817




13817












  • Check out here: regex101.com/r/fFIS01/1
    – Masious
    Nov 14 at 2:23












  • That regex doesn´t seem to match anything. I have a small problem with the decimal input (I need to block out certain contexts: number.number.number for instance or number +.). My previous methods didn´t seem to accomplish this, so I want to see if I could check for the number in state before adding a decimal.
    – Hernan Ariel
    Nov 14 at 2:26










  • Can you provide a jsFiddle?
    – GMaiolo
    Nov 14 at 2:40










  • Yeah. Basically this, but with the buttons mapped from arrays: codesandbox.io/s/z293rk7y2m
    – Hernan Ariel
    Nov 14 at 2:53










  • @HernanAriel - number +. is a valid context right? it should be treated as number + 0.number ?
    – Ramesh
    Nov 14 at 3:35


















  • Check out here: regex101.com/r/fFIS01/1
    – Masious
    Nov 14 at 2:23












  • That regex doesn´t seem to match anything. I have a small problem with the decimal input (I need to block out certain contexts: number.number.number for instance or number +.). My previous methods didn´t seem to accomplish this, so I want to see if I could check for the number in state before adding a decimal.
    – Hernan Ariel
    Nov 14 at 2:26










  • Can you provide a jsFiddle?
    – GMaiolo
    Nov 14 at 2:40










  • Yeah. Basically this, but with the buttons mapped from arrays: codesandbox.io/s/z293rk7y2m
    – Hernan Ariel
    Nov 14 at 2:53










  • @HernanAriel - number +. is a valid context right? it should be treated as number + 0.number ?
    – Ramesh
    Nov 14 at 3:35
















Check out here: regex101.com/r/fFIS01/1
– Masious
Nov 14 at 2:23






Check out here: regex101.com/r/fFIS01/1
– Masious
Nov 14 at 2:23














That regex doesn´t seem to match anything. I have a small problem with the decimal input (I need to block out certain contexts: number.number.number for instance or number +.). My previous methods didn´t seem to accomplish this, so I want to see if I could check for the number in state before adding a decimal.
– Hernan Ariel
Nov 14 at 2:26




That regex doesn´t seem to match anything. I have a small problem with the decimal input (I need to block out certain contexts: number.number.number for instance or number +.). My previous methods didn´t seem to accomplish this, so I want to see if I could check for the number in state before adding a decimal.
– Hernan Ariel
Nov 14 at 2:26












Can you provide a jsFiddle?
– GMaiolo
Nov 14 at 2:40




Can you provide a jsFiddle?
– GMaiolo
Nov 14 at 2:40












Yeah. Basically this, but with the buttons mapped from arrays: codesandbox.io/s/z293rk7y2m
– Hernan Ariel
Nov 14 at 2:53




Yeah. Basically this, but with the buttons mapped from arrays: codesandbox.io/s/z293rk7y2m
– Hernan Ariel
Nov 14 at 2:53












@HernanAriel - number +. is a valid context right? it should be treated as number + 0.number ?
– Ramesh
Nov 14 at 3:35




@HernanAriel - number +. is a valid context right? it should be treated as number + 0.number ?
– Ramesh
Nov 14 at 3:35












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










In Handle click, try replacing the default to below



  handleClick(evt) {
const id = evt.target.id;
let result = evt.target.value;

switch (id) {
case "clear":
this.setState({ value: "0" });
break;
case "equals":
this.setState(prevState => ({
value: math.eval(this.state.value)
}));
break;

default:
this.setState(prevState => {
const numbers = prevState.value.split(/[+-*=]/g);
const lastNumber = numbers[numbers.length - 1];
if (result === ".") {
console.log((lastNumber.match(/./g) || ).length);
if (lastNumber.length === 0) {
result = ""; //Remove if first charracter is decimal
}
if ((lastNumber.match(/./g) || ).length > 0) {
result = "";
}
}
let value = `${prevState.value}${result}`
.replace(/([+-*=])([+-*=])/g, "$2")
.replace(/^0+(?=[1-9])/, "")
.replace(/^0+(?=.)/, "0")
.replace(/^0+B/, "");

return {
value
};
});
}
}





share|improve this answer























  • I tried it, but whenever I try to trigger one of the wrong contexts (for instance, 0..), I get: Error: "result" is read-only
    – Hernan Ariel
    Nov 14 at 3:43










  • Change the const in result to let
    – Ramesh
    Nov 14 at 3:52










  • It sort of works, but it still allows: .+.+. and things like that
    – Hernan Ariel
    Nov 14 at 3:57










  • Updated code. pls check
    – Ramesh
    Nov 14 at 4:07










  • Seems to work. What does this line do? ((lastNumber.match(/./g) || ).length > 0)
    – Hernan Ariel
    Nov 14 at 4:14


















up vote
0
down vote













The method you're looking for is includes



EDIT per your additional information:



var positions = ;
for(i = 0; i < this.state.value.length; i++) {
if(this.state.value.charAt(i) === result) {
positions.push(i);
}
}


You'll have to edit to fit your specific needs (I get the impression you're looking for something specific which you haven't overtly stated) but I imagine this gets you past your obstacle.






share|improve this answer























  • Thanks for the reply. I tried that, but I could never get the decimal to work properly. I need to know the position as well, not just if it´s present.
    – Hernan Ariel
    Nov 14 at 3:07












  • Updated for you
    – Jake Roby
    Nov 14 at 3:15











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292245%2fhow-can-i-check-whether-an-item-already-exists-in-state%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










In Handle click, try replacing the default to below



  handleClick(evt) {
const id = evt.target.id;
let result = evt.target.value;

switch (id) {
case "clear":
this.setState({ value: "0" });
break;
case "equals":
this.setState(prevState => ({
value: math.eval(this.state.value)
}));
break;

default:
this.setState(prevState => {
const numbers = prevState.value.split(/[+-*=]/g);
const lastNumber = numbers[numbers.length - 1];
if (result === ".") {
console.log((lastNumber.match(/./g) || ).length);
if (lastNumber.length === 0) {
result = ""; //Remove if first charracter is decimal
}
if ((lastNumber.match(/./g) || ).length > 0) {
result = "";
}
}
let value = `${prevState.value}${result}`
.replace(/([+-*=])([+-*=])/g, "$2")
.replace(/^0+(?=[1-9])/, "")
.replace(/^0+(?=.)/, "0")
.replace(/^0+B/, "");

return {
value
};
});
}
}





share|improve this answer























  • I tried it, but whenever I try to trigger one of the wrong contexts (for instance, 0..), I get: Error: "result" is read-only
    – Hernan Ariel
    Nov 14 at 3:43










  • Change the const in result to let
    – Ramesh
    Nov 14 at 3:52










  • It sort of works, but it still allows: .+.+. and things like that
    – Hernan Ariel
    Nov 14 at 3:57










  • Updated code. pls check
    – Ramesh
    Nov 14 at 4:07










  • Seems to work. What does this line do? ((lastNumber.match(/./g) || ).length > 0)
    – Hernan Ariel
    Nov 14 at 4:14















up vote
1
down vote



accepted










In Handle click, try replacing the default to below



  handleClick(evt) {
const id = evt.target.id;
let result = evt.target.value;

switch (id) {
case "clear":
this.setState({ value: "0" });
break;
case "equals":
this.setState(prevState => ({
value: math.eval(this.state.value)
}));
break;

default:
this.setState(prevState => {
const numbers = prevState.value.split(/[+-*=]/g);
const lastNumber = numbers[numbers.length - 1];
if (result === ".") {
console.log((lastNumber.match(/./g) || ).length);
if (lastNumber.length === 0) {
result = ""; //Remove if first charracter is decimal
}
if ((lastNumber.match(/./g) || ).length > 0) {
result = "";
}
}
let value = `${prevState.value}${result}`
.replace(/([+-*=])([+-*=])/g, "$2")
.replace(/^0+(?=[1-9])/, "")
.replace(/^0+(?=.)/, "0")
.replace(/^0+B/, "");

return {
value
};
});
}
}





share|improve this answer























  • I tried it, but whenever I try to trigger one of the wrong contexts (for instance, 0..), I get: Error: "result" is read-only
    – Hernan Ariel
    Nov 14 at 3:43










  • Change the const in result to let
    – Ramesh
    Nov 14 at 3:52










  • It sort of works, but it still allows: .+.+. and things like that
    – Hernan Ariel
    Nov 14 at 3:57










  • Updated code. pls check
    – Ramesh
    Nov 14 at 4:07










  • Seems to work. What does this line do? ((lastNumber.match(/./g) || ).length > 0)
    – Hernan Ariel
    Nov 14 at 4:14













up vote
1
down vote



accepted







up vote
1
down vote



accepted






In Handle click, try replacing the default to below



  handleClick(evt) {
const id = evt.target.id;
let result = evt.target.value;

switch (id) {
case "clear":
this.setState({ value: "0" });
break;
case "equals":
this.setState(prevState => ({
value: math.eval(this.state.value)
}));
break;

default:
this.setState(prevState => {
const numbers = prevState.value.split(/[+-*=]/g);
const lastNumber = numbers[numbers.length - 1];
if (result === ".") {
console.log((lastNumber.match(/./g) || ).length);
if (lastNumber.length === 0) {
result = ""; //Remove if first charracter is decimal
}
if ((lastNumber.match(/./g) || ).length > 0) {
result = "";
}
}
let value = `${prevState.value}${result}`
.replace(/([+-*=])([+-*=])/g, "$2")
.replace(/^0+(?=[1-9])/, "")
.replace(/^0+(?=.)/, "0")
.replace(/^0+B/, "");

return {
value
};
});
}
}





share|improve this answer














In Handle click, try replacing the default to below



  handleClick(evt) {
const id = evt.target.id;
let result = evt.target.value;

switch (id) {
case "clear":
this.setState({ value: "0" });
break;
case "equals":
this.setState(prevState => ({
value: math.eval(this.state.value)
}));
break;

default:
this.setState(prevState => {
const numbers = prevState.value.split(/[+-*=]/g);
const lastNumber = numbers[numbers.length - 1];
if (result === ".") {
console.log((lastNumber.match(/./g) || ).length);
if (lastNumber.length === 0) {
result = ""; //Remove if first charracter is decimal
}
if ((lastNumber.match(/./g) || ).length > 0) {
result = "";
}
}
let value = `${prevState.value}${result}`
.replace(/([+-*=])([+-*=])/g, "$2")
.replace(/^0+(?=[1-9])/, "")
.replace(/^0+(?=.)/, "0")
.replace(/^0+B/, "");

return {
value
};
});
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 at 4:06

























answered Nov 14 at 3:33









Ramesh

9,71423777




9,71423777












  • I tried it, but whenever I try to trigger one of the wrong contexts (for instance, 0..), I get: Error: "result" is read-only
    – Hernan Ariel
    Nov 14 at 3:43










  • Change the const in result to let
    – Ramesh
    Nov 14 at 3:52










  • It sort of works, but it still allows: .+.+. and things like that
    – Hernan Ariel
    Nov 14 at 3:57










  • Updated code. pls check
    – Ramesh
    Nov 14 at 4:07










  • Seems to work. What does this line do? ((lastNumber.match(/./g) || ).length > 0)
    – Hernan Ariel
    Nov 14 at 4:14


















  • I tried it, but whenever I try to trigger one of the wrong contexts (for instance, 0..), I get: Error: "result" is read-only
    – Hernan Ariel
    Nov 14 at 3:43










  • Change the const in result to let
    – Ramesh
    Nov 14 at 3:52










  • It sort of works, but it still allows: .+.+. and things like that
    – Hernan Ariel
    Nov 14 at 3:57










  • Updated code. pls check
    – Ramesh
    Nov 14 at 4:07










  • Seems to work. What does this line do? ((lastNumber.match(/./g) || ).length > 0)
    – Hernan Ariel
    Nov 14 at 4:14
















I tried it, but whenever I try to trigger one of the wrong contexts (for instance, 0..), I get: Error: "result" is read-only
– Hernan Ariel
Nov 14 at 3:43




I tried it, but whenever I try to trigger one of the wrong contexts (for instance, 0..), I get: Error: "result" is read-only
– Hernan Ariel
Nov 14 at 3:43












Change the const in result to let
– Ramesh
Nov 14 at 3:52




Change the const in result to let
– Ramesh
Nov 14 at 3:52












It sort of works, but it still allows: .+.+. and things like that
– Hernan Ariel
Nov 14 at 3:57




It sort of works, but it still allows: .+.+. and things like that
– Hernan Ariel
Nov 14 at 3:57












Updated code. pls check
– Ramesh
Nov 14 at 4:07




Updated code. pls check
– Ramesh
Nov 14 at 4:07












Seems to work. What does this line do? ((lastNumber.match(/./g) || ).length > 0)
– Hernan Ariel
Nov 14 at 4:14




Seems to work. What does this line do? ((lastNumber.match(/./g) || ).length > 0)
– Hernan Ariel
Nov 14 at 4:14












up vote
0
down vote













The method you're looking for is includes



EDIT per your additional information:



var positions = ;
for(i = 0; i < this.state.value.length; i++) {
if(this.state.value.charAt(i) === result) {
positions.push(i);
}
}


You'll have to edit to fit your specific needs (I get the impression you're looking for something specific which you haven't overtly stated) but I imagine this gets you past your obstacle.






share|improve this answer























  • Thanks for the reply. I tried that, but I could never get the decimal to work properly. I need to know the position as well, not just if it´s present.
    – Hernan Ariel
    Nov 14 at 3:07












  • Updated for you
    – Jake Roby
    Nov 14 at 3:15















up vote
0
down vote













The method you're looking for is includes



EDIT per your additional information:



var positions = ;
for(i = 0; i < this.state.value.length; i++) {
if(this.state.value.charAt(i) === result) {
positions.push(i);
}
}


You'll have to edit to fit your specific needs (I get the impression you're looking for something specific which you haven't overtly stated) but I imagine this gets you past your obstacle.






share|improve this answer























  • Thanks for the reply. I tried that, but I could never get the decimal to work properly. I need to know the position as well, not just if it´s present.
    – Hernan Ariel
    Nov 14 at 3:07












  • Updated for you
    – Jake Roby
    Nov 14 at 3:15













up vote
0
down vote










up vote
0
down vote









The method you're looking for is includes



EDIT per your additional information:



var positions = ;
for(i = 0; i < this.state.value.length; i++) {
if(this.state.value.charAt(i) === result) {
positions.push(i);
}
}


You'll have to edit to fit your specific needs (I get the impression you're looking for something specific which you haven't overtly stated) but I imagine this gets you past your obstacle.






share|improve this answer














The method you're looking for is includes



EDIT per your additional information:



var positions = ;
for(i = 0; i < this.state.value.length; i++) {
if(this.state.value.charAt(i) === result) {
positions.push(i);
}
}


You'll have to edit to fit your specific needs (I get the impression you're looking for something specific which you haven't overtly stated) but I imagine this gets you past your obstacle.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 at 3:14

























answered Nov 14 at 3:05









Jake Roby

5,23211126




5,23211126












  • Thanks for the reply. I tried that, but I could never get the decimal to work properly. I need to know the position as well, not just if it´s present.
    – Hernan Ariel
    Nov 14 at 3:07












  • Updated for you
    – Jake Roby
    Nov 14 at 3:15


















  • Thanks for the reply. I tried that, but I could never get the decimal to work properly. I need to know the position as well, not just if it´s present.
    – Hernan Ariel
    Nov 14 at 3:07












  • Updated for you
    – Jake Roby
    Nov 14 at 3:15
















Thanks for the reply. I tried that, but I could never get the decimal to work properly. I need to know the position as well, not just if it´s present.
– Hernan Ariel
Nov 14 at 3:07






Thanks for the reply. I tried that, but I could never get the decimal to work properly. I need to know the position as well, not just if it´s present.
– Hernan Ariel
Nov 14 at 3:07














Updated for you
– Jake Roby
Nov 14 at 3:15




Updated for you
– Jake Roby
Nov 14 at 3:15


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292245%2fhow-can-i-check-whether-an-item-already-exists-in-state%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?