8

I am trying to access an object array file within src folder eg: data.js(Object array only) this file into my app.js(react component)

in first scenario 1.I have tried this problem using react in

src--[app.js(component)/data.js(object array)].

When I was run it shows me an error like

(TypeError: _data__WEBPACK_IMPORTED_MODULE_1___default.a.map is not a function)means null array/undefined.

in second scenarios 2. when I add object array in app.js within the same page its shows me perfect result. without an error but trying from another file like data.js it taking null array I have used to stringify() and JSON parser but no result

Object array file data.js ->

const datas=[
    {
    "id":"1",
    "firstname":"sam",
    "lastname":"parkar"
    },
    {
    "id":"2",
    "firstname":"julee",
    "lastname":"fransic"
    }
    ];

react component app.js ->

import React from 'react';
import  datas from './data';
import  DataInfo from './DataInfo';
function App () {
 const appdata=datas.map( inner => inner.id + inner.firstname + inner.lastname)
//print on console
 console.log(appdata)
    return (
     <div className="App">
           <p>App js page</p>

              {appdata} 

      </div>
    )
  }
export default App;

error ->

TypeError: _data__WEBPACK_IMPORTED_MODULE_1___default.a.map is not a function
  21 | return (  
  22 |   
  23 |   
> 24 |   <div className="App">  
     |  ^  25 |   
  26 |        <p>App js page</p>  

actual result:-
App js page

1samparkar2juleefransic
and on console (2) ["1samparkar", "2juleefransic"] 0: "1samparkar" 1: "2juleefransic"

1
  • 2
    Sidenote: "Data" is already a plural word, "Datum" being the singular form of the word. So "Data" never takes an "s". Commented May 30, 2021 at 14:31

3 Answers 3

22

Make sure you export the datas correctly

export const datas=[
  {
    "id": "1",
    "firstname": "sam",
    "lastname": "parkar"
  },
  {
    "id": "2",
    "firstname": "julee",
    "lastname": "fransic"
  }
];

And in app.js call it like this:

import  {datas} from './data';
Sign up to request clarification or add additional context in comments.

2 Comments

Having imported datas, how would you call just "firstname"s?
@Deborah you want to get an array of first names? then const firstNames = datas.map( e => e.firstname)
2

You can use JSON file like this:

datas.json

[
    {
    "id":"1",
    "firstname":"sam",
    "lastname":"parkar"
    },
    {
    "id":"2",
    "firstname":"julee",
    "lastname":"fransic"
    }
]

In app.js:

import datas from './datas.json';

Comments

1

If you are using JSON file then save that file as datas.json

Now in your app.js file use <datas/> instead of {datas}.

you can use {datas} when you are using it in a jsx attribute. for example-

<textarea name="JSON" value={datas} />.

but in your case, you should use <datas />.

1 Comment

I have tried your solution and cheers it works thanks a lot! +1

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.