1

I'm trying to upload csv file and display it on Table.

So I'm trying to display info data on DataTable and I got an error info[0].slice is not a function.

So I tried to comment info[0].slice this out . then it works but I can't get the column.label when i uncomment info[0].slice after i upload a file.

I don't know what happens behind it so i can't figure out .

So How can i make columns work ??

my code is like this :

  const [info, setInfo] = useState([{}]);
  const handleOnDrop = droppedItems => {
    const infos = droppedItems.map(item => item.data);

    setInfo([...infos]);
  };

const DataTable = ({ info }) => {
  // const columns = []; //this is what i was trying to do to make columns work afterwards.

  const columns = info[0]
    .slice(undefined)
    .map(inf => [{ id: inf, label: inf, minWidth: 170, align: "left" }]);

<TableRow>
              {columns.map(column => (
                <TableCell
                  key={column.id}
                  align={column.align}
                  style={{ minWidth: column.minWidth }}
                >
                  <select name="" id="">
                    <option value="3" selected>
              
                    </option>
                    <option value="0">x</option>
                    <option value="1">y</option>
                    <option value="2">label</option>
                    
                  </select>{" "}
                  <p>{column.label}</p>
                </TableCell>
              
this should be the results when it works !

enter image description here

6
  • 1
    what are you trying to accomplish with slice(undefined)? Commented Jun 17, 2021 at 3:14
  • I'm trying to get all Array from info[0] with slice(undefined) Commented Jun 17, 2021 at 3:17
  • 1
    what is info[0] before slice? Commented Jun 17, 2021 at 3:18
  • it is (4) ["age ", "name", "phone", "city"] that is from a csv file (it's console results) Commented Jun 17, 2021 at 3:20
  • I should get the array from random data so i tried to do it like that Commented Jun 17, 2021 at 3:20

1 Answer 1

1

From what I can see in your code - info is an array of objects.

Therefore info[0] is an object (not an array) and it doesn't have a splice method.

I'm not sure what's the purpose of slice(undefined) in your code. Could you please explain?

I would suggest you create a Minimal Working Sample, which reproduces your issue in JSFiddle. This will make it easier for us to help you out.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm trying to get all Array from info[0] with slice(undefined) to get all the Array from uploaded csv file .

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.