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?

5
  • That's not JSON. What is the difference between JSON and Object Literal Notation? Commented Jan 5, 2019 at 13:56
  • Please add the "logic" that determines the expected output? Why are some values missing? Where is Hygh coming from? Commented Jan 5, 2019 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. Commented Jan 5, 2019 at 14:00
  • yes one value per row @Jeto Commented Jan 5, 2019 at 14:03
  • Updates question now @Andreas Commented Jan 5, 2019 at 14:06

2 Answers 2

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);

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

1 Comment

Is it any way to get array of only for some values from responseJson.data, Not all.
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)

Comments

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.