3

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

["23435","87567", "34536","45234","34532","65365"]
2
  • 6
    That's an array not a JSON format file. Those are the only contents in the file? Commented Feb 6, 2022 at 16:34
  • oh so it's just an array ? . yes the content is just like that. Commented Feb 6, 2022 at 16:44

2 Answers 2

2

What you want is to find a value in an array.

You can use includes

    const array = ["23435","87567", "34536","45234","34532","65365"]

    const aConstant = "23435"

    return (<div>{ array.includes(aConstant) ? 'a' : 'b' }</div>)

Same thing with indexOf

    const array = ["23435","87567", "34536","45234","34532","65365"]

    const aConstant = "23435"

    return (<div>{ array.indexOf(aConstant) !== -1 ? 'a' : 'b' }</div>)

You can also try filter

    const array = ["23435","87567", "34536","45234","34532","65365"]

    const aConstant = "23435"

    return (<div>{ Boolean(array.filter( x => x === aConstant)) ? 'a' : 'b' }</div>)

And even find

    const array = ["23435","87567", "34536","45234","34532","65365"]

    const aConstant = "23435"

    return (<div>{ array.find( x => x === aConstant) ? 'a' : 'b' }</div>)
Sign up to request clarification or add additional context in comments.

Comments

0

Since this is not a proper JSON format file, you can load it in simply as a string:

import arrayFile from './arrayFile.txt';

Then, if your file is truly just an array hidden with nothing else, you can do something like this to get it into an array format:

const newArray = JSON.parse(arrayFile);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.