0

I am trying to get a structure like

var tableData= [
  ['Hfbj'],
  ['Hygh'],
  [6],
  ['mayur'],
  [2563458952]
]

Here is my JSON data:

data:{
  "address"': "Hfbj"
  "id": 6
  "landmark": "Hygh"
  "name": "mayur"
  "phone": 2563458952
  "title": "aaa"
}

I am using react-native-table-component in which that type of structure needed. For that, I am doing the following but it showing data.map is not function and undefined.

let newdata = this.state.tableData[0].push(
  [responseJson.data.title],
  [responseJson.data.title]
);

this.setState({
  tableData: newdata
});

How can I achieve it?

Anas Abu Farraj
  • 1,540
  • 4
  • 23
  • 31
Mayuresh Patil
  • 2,072
  • 1
  • 21
  • 44
  • That's _not_ [JSON](http://json.org). [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Jan 05 '19 at 13:56
  • Please add the "logic" that determines the expected output? Why are some values missing? Where is `Hygh` coming from? – Andreas Jan 05 '19 at 13:58
  • Also, are you sure you want one value per row in your table? Because that's what this kind of format would produce, according to the docs you linked. – Jeto Jan 05 '19 at 14:00
  • yes one value per row @Jeto – Mayuresh Patil Jan 05 '19 at 14:03
  • Updates question now @Andreas – Mayuresh Patil Jan 05 '19 at 14:06

2 Answers2

1

.map Is for arrays, whereas your responseJson.data is an Object. To get turn that Object into an array of its values you can do Object.values(responseJson.data)

dan
  • 1,198
  • 8
  • 25
1

You could make use of Object.values and Array.map:

var reponseJson = {
  data: {
    "address": "Hfbj",
    "id": 6,
    "landmark": "Hygh",
    "name": "mayur",
    "phone": 2563458952,
    "title": "aaa"
  }
};

var newData = Object.values(reponseJson.data)
  .map(item => [item]);

console.log(newData);

Note that I used the responseJson name to match your question, but as @Andreas pointed out, this is an object, not JSON.

If you need only certain columns (as requested in the comments below), use Object.keys and Array.filter on them before rebuilding the array:

var reponseJson = {
  data: {
    "address": "Hfbj",
    "id": 6,
    "landmark": "Hygh",
    "name": "mayur",
    "phone": 2563458952,
    "title": "aaa"
  }
};

var keysToKeep = ['address', 'landmark', 'title'];

var newData = Object.keys(reponseJson.data)
  .filter(key => keysToKeep.includes(key))
  .map(key => [reponseJson.data[key]]);

console.log(newData);
Jeto
  • 14,596
  • 2
  • 32
  • 46