Hi I am very new to reactjs. I have a hosted json file as below. Json object looks like below with Around 500+ values:
[
{
"timestamp": "2020-03-23T14:00:00.000Z",
"symbol": "CMG",
"name": "Chipotle Mexican Grill",
"industry": "Consumer Discretionary",
"open": 561.31,
"high": 602.265,
"low": 551.21,
"close": 588.71,
"volumes": 1281710
},
{
"timestamp": "2020-03-23T14:00:00.000Z",
"symbol": "MTD",
"name": "Mettler Toledo",
"industry": "Health Care",
"open": 615.51,
"high": 652.94,
"low": 592.64,
"close": 641.845,
"volumes": 157358
},
{
"timestamp": "2020-03-23T14:00:00.000Z",
"symbol": "AZO",
"name": "AutoZone Inc",
"industry": "Consumer Discretionary",
"open": 711.65,
"high": 741.865,
"low": 684.91,
"close": 723.22,
"volumes": 435135
},
{
"timestamp": "2020-03-23T14:00:00.000Z",
"symbol": "GOOGL",
"name": "Alphabet Inc Class A",
"industry": "Information Technology",
"open": 1056.37,
"high": 1066.91,
"low": 1008.87,
"close": 1054.13,
"volumes": 4116970
},
{
"timestamp": "2020-03-23T14:00:00.000Z",
"symbol": "GOOG",
"name": "Alphabet Inc Class C",
"industry": "Information Technology",
"open": 1061.32,
"high": 1071.32,
"low": 1013.54,
"close": 1056.62,
"volumes": 4001750
},
{
"timestamp": "2020-03-23T14:00:00.000Z",
"symbol": "AMZN",
"name": "Amazon.com Inc",
"industry": "Consumer Discretionary",
"open": 1827.75,
"high": 1919.4,
"low": 1812,
"close": 1902.83,
"volumes": 7701940
}
]
I want array of unique industry item from json object. I am trying to create unique array from json object industry and pass it to html . Plan is to use it for searching. But I can't seem to map and store industry object in array. Here is my code :SelectIndustry.js
function SelectIndustry() {
const [industry,setindustry]=useState('');
// fetch jason and store industry
useEffect (()=>{
fetch('http://localhost:3000/all')
.then((rs)=>rs.json())
.then((rs)=>{rs.map((info)=>{
return {industry:info.industry}})})
},[])
// trying to get remove duplicate item from array
let uniqueindustry = [...new Set(industry)]
console.log(uniqueindustry)
// mapping array item to select option
var select = document.getElementById("selectIndustry");
for(var i = 0; i <uniqueindustry.length; i++) {
var opt = uniqueindustry[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
return (
<select id='selectIndustry'>
<option>Choose a Industry</option>
</select>
);
}
export default SelectIndustry;
I have no idea how to achieve this. I am happy to rewrite entire code if needed. Please help