0

I'm a beginner in vuejs and I'd like to map my data response in a two-dimensional table.

This is my data:

[{
  "origine": "",
  "nb": 15
}, {
  "origine": "?",
  "nb": 18
}, {
  "origine": "?L",
  "nb": 1
}, {
  "origine": "G",
  "nb": 298
}, {
  "origine": "I",
  "nb": 51
}, {
  "origine": "L",
  "nb": 1735
}, {
  "origine": "L?",
  "nb": 1
}, {
  "origine": "O",
  "nb": 4
}]

After mapping I want the data to be like this:

[
  ['', 15],
  ['?', 18],
  ['?L', 1],
  ['G', 298],
  ['I', 51],
  ['L', 1735],
  ['L?', 1],
  ['O', 4]
]

Or like this :

[
  '': 15,
  '?': 18,
  '?L': 1, 
  'G': 298, 
  'I': 51, 
  'L': 1735, 
  'L?': 1, 
  'O': 4
]

So far I've written this:

getResultcivitas(localisation) {
  axios
    .get('../api/civitasorigine/' + this.searchInputcivitas)
    .then(response => {this.origines = response.data.map (x => x.origine)})
}

I've find this but I don't know how to spell it out in my axios query.

map = origines.map(obj => {
  var rObj = {};
  rObj[obj.origine] = obj.nb;
  return rObj;
});

2 Answers 2

3

Just return an array from the map functions' callback with item.origine and item.nb

let data = [{
  "origine": "",
  "nb": 15
}, {
  "origine": "?",
  "nb": 18
}, {
  "origine": "?L",
  "nb": 1
}, {
  "origine": "G",
  "nb": 298
}, {
  "origine": "I",
  "nb": 51
}, {
  "origine": "L",
  "nb": 1735
}, {
  "origine": "L?",
  "nb": 1
}, {
  "origine": "O",
  "nb": 4
}]

let mappedData = data.map(item => {
  return [item.origine, item.nb];
})

console.log(mappedData)

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

1 Comment

I have formulated my second question about the objects in VueJs on this topic. Can you help me stackoverflow.com/questions/62531807/…
2

Well, you literally have all the answers, you just need to combine them:

axios
  .get('../api/civitasorigine/' + this.searchInputcivitas)
  .then(response => {
    this.origines = response.data.map(obj => {
      const rObj = {};
      rObj[obj.origine] = obj.nb;
      return rObj;
    })
  })

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.