0

I want import an array from a json file which only contains this array. in this app, I want to perform a search on this array. so it must be recieved in the array formating.

this is a part of long array exsisting in cities.json :

cities.json:

[ "Aberdeen", "Abilene", "Akron", "Albany", "Albuquerque", "Alexandria", "Allentown", "Amarillo", ..., ..., ..., "Yonkers", "York", "Youngstown" ]

enter image description here

In App code, I have displayed the input of the JSON file inside the p tag, Just to see what I get.

index.jsx:

import React from "react"; import ReactDOM from "react-dom"; import text from "./cities.json"; class App extends React.Component{ constructor(props) { super(props); this.state = { value: '', hint: '' } } handleChange = (e) => { this.setState({ value: e.target.value }); } render() { return ( <div> <p>{text}</p> <input value={this.state.value} placeholder={this.state.hint} onChange={this.handleChange}/> </div> ); } } ReactDOM.render(<App/>, document.getElementById("root")); 

But I see output is only a text.

enter image description here

How to I recieved that in the array form?

3 Answers 3

1

It may already be in array form, that's probably just how react renders arrays. Try an array operation on the value.

text.filter(city => city.startsWith('A')) 

If that doesnt work you may need to parse the JSON first.

let parsedText = JSON.parse(text) parsedText.filter((city) => city.startsWith("A")); 
Sign up to request clarification or add additional context in comments.

2 Comments

Why "A"? whole items not begined with "A"! what do you mean?
Because multiple items begin with A. If you to match a single, whole, item u can do '''text.filter(city => city === "Albuquerque")'''
1

It's already an array. If you want to see it in array format you can use the console.log(text) out of the render function.

Alternatively, you can show it on the page using <p>{JSON.stringify(text)}</p>

2 Comments

Is it to this mean that if I to recieve "text" with 'JSON.stringify', still is in the array formating? and I can performe search on it?
@ArmanEbrahimi No. JSON.stringify converts the array to plain text but the text will look like the original array. If you want to perform array operations you can use the original array named text in your question. Actually text is a bit confusing name in this code. It should be something more clear like cities.
0

Due to the fact "text" is an array, you have to be specific about the value you are presenting.

So it this case:

{text} // array 

So you are trying to render the array itself.

If you want to check first if it's working, try to render

{text[0]} 

If it's working, you can map, filter or make any operation you want on the array.

Let me know if it helps

2 Comments

oooh yes so thanks. it worked. but I was surprised. how do? If react sees this in the array form, why in output not was "," between items, when that I displayed whole text?
Im pretty sure its due to the nature of jsx. Because if you provide an array, jsx doesn't know how to handle the render. Same for objects as well (but im not 100% sure)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.