I want to retrieve data from a json file and then match it with the value of an existing variable and if the variable value matches the data in json it will display a message “a” and if it does not match it will display a message “b”.
the json file is like this
JavaScript
x
2
1
["23435","87567", "34536","45234","34532","65365"]
2
Advertisement
Answer
What you want is to find a value in an array.
You can use includes
JavaScript
1
6
1
const array = ["23435","87567", "34536","45234","34532","65365"]
2
3
const aConstant = "23435"
4
5
return (<div>{ array.includes(aConstant) ? 'a' : 'b' }</div>)
6
Same thing with indexOf
JavaScript
1
6
1
const array = ["23435","87567", "34536","45234","34532","65365"]
2
3
const aConstant = "23435"
4
5
return (<div>{ array.indexOf(aConstant) !== -1 ? 'a' : 'b' }</div>)
6
You can also try filter
JavaScript
1
6
1
const array = ["23435","87567", "34536","45234","34532","65365"]
2
3
const aConstant = "23435"
4
5
return (<div>{ Boolean(array.filter( x => x === aConstant)) ? 'a' : 'b' }</div>)
6
And even find
JavaScript
1
6
1
const array = ["23435","87567", "34536","45234","34532","65365"]
2
3
const aConstant = "23435"
4
5
return (<div>{ array.find( x => x === aConstant) ? 'a' : 'b' }</div>)
6