0

I'm working on a React Native application that involves choosing words from a list of 200,000. I already have this list of words, but I'm having trouble putting it into my application. At first, I wanted to put them in a separate file and read them into an array, but React Native does not have access to the fs node module. I tried copying and pasting them in directly, but this is messy and cumbersome. Are there any other ways that this can be done? Any help would be appreciated!

dannnny
  • 117
  • 1
  • 7

1 Answers1

0

You can simply Store the data in json in a seperate file, And then import it using the import statement.

Here is an example.

data.json file

{
  "name": "saroj",
  "age": 180
}

app.js

import json from "./data.json";

function App() {
  console.log(json);
  return <View style={styles.app}></View>;
}

the output is :

{name: "saroj", age: 180}

According to question

you can store the list of over 200,000 words in an array in a json file like wise:

{
"wordList": [
"word1",
"word2",
"word3",
..
...
....
"word200000"

]
} 

thank you.

  • Thank you! This is very helpful. I'm still having a bit of trouble understanding how to add the list of words to the json though. Would I put them in an array in a key-value pair? Ideally, the words would each be on their own line – dannnny Sep 03 '22 at 04:19
  • @dannnny you can also directly export array instead of json. – Servesh Chaturvedi Sep 03 '22 at 04:37
  • @ServeshChaturvedi That makes sense! The problem I'm having with this is that if too many things are on the same line, VS Code stops tokenizing them and doesn't process them well. I think that they will probably all have to be on their own line. – dannnny Sep 03 '22 at 04:55
  • @SarojRegmi forgot to tag you above, sorry! – dannnny Sep 03 '22 at 04:56
  • You can use other tools like Notepad++ or MS Excel to format your array @dannnny – Servesh Chaturvedi Sep 03 '22 at 04:58
  • @ServeshChaturvedi exporting as an array would also be a good approach, but when the data is a little big i don't think so making a js file to hold it in an array would be a good idea. And dannnny i have edited the answer to suite your requirements. –  Sep 03 '22 at 07:59
  • @dannnny, its fine, would you mind upvoting and accepting the answer, if it was helpful it will also help other peoples too. –  Sep 03 '22 at 08:04
  • 1
    @SarojRegmi the code u modified, is that a valid json? I suppose you have to put in a key and then value can be an array. – Servesh Chaturvedi Sep 03 '22 at 09:55
  • @ServeshChaturvedi sry my bad, let me fix it out real quick, thank you for pointing it out. –  Sep 03 '22 at 10:03
  • Just accepted! @SarojRegmi Thank you for your help! I think the answer you gave will work now. I don't have the ability to upvote yet, but I'll come back and upvote your answer and comments when I do! – dannnny Sep 03 '22 at 15:41
  • @ServeshChaturvedi Could only tag one person above, but same to you! – dannnny Sep 03 '22 at 15:42