0

I have java script array of objects read from html table like below depId represent the row number, branchId represent column number and val it linked with check box (vue js v-model) 3*3 table data:

 permissions=   [
    [{depId:1,branchId:1,val:true},{depId:1,branchId:2,val:true},{depId:1,branchId:3}],
    [{depId:2,branchId:1},{depId:2,branchId:2},{depId:2,branchId:3}],
    [{depId:3,branchId:1},{depId:3,branchId:2},{depId:3,branchId:3,val:true}]
    ]

I need to send this data to axios API, but the data should be in below format

data[0][branches][0]=1
data[0][branches][1]=2
data[0][department]=1  

data[1][branches][0]=3
data[1][department]=3

I tried something like this but it have problems (the data sent in wrong indexes)

let data={};
   permissions.forEach((row, i) => {
      row.forEach((col, j) => {
        if (col["val"] === true) {
          data[`data[${i}][branches][${j}]`] = col.branchId;
          data[`data[${i}][department]`] = col.deptId;
        }
      });
    });
        console.log(data);

how the loop should be to send the data in correct way?

the current result is

"data[0][branches][0]": 1,
  "data[0][department]": 1,
  "data[0][branches][1]": 2,
  "data[2][branches][2]": 3,
  "data[2][department]": 3
13
  • but it have problems what are the problems? Commented Oct 19, 2020 at 5:42
  • the data sent in wrong indexes @JaromandaX Commented Oct 19, 2020 at 5:43
  • Yeah, first problem: your indexes are going from 1 to 3 instead of 0 to 2. Commented Oct 19, 2020 at 5:49
  • Is your re-referencing data construct "data[data[${i}][branches][${j}]]" really the way you want to do the job? I don't even want to understand that one... it's bad readability for sure. Commented Oct 19, 2020 at 5:51
  • The API built in this way @JanosVinceller Commented Oct 19, 2020 at 5:53

1 Answer 1

1

You forgot a couple of commas in your permissions object. The next problem is that you were trying to check for departmentId in permissions, but it's actually depId there. The next thing is that you do not need to define and track i and j, they are conveniently provided to you in a forEach function as the second argument that is passed to the running function.

Here is a working version of what you were trying to achieve:

permissions = [
  [{
    depId: 1,
    branchId: 1,
    val: true
  }, {
    depId: 1,
    branchId: 2,
    val: true
  }, {
    depId: 1,
    branchId: 3
  }],
  [{
    depId: 2,
    branchId: 1
  }, {
    depId: 2,
    branchId: 2
  }, {
    depId: 2,
    branchId: 3
  }],
  [{
    depId: 3,
    branchId: 1
  }, {
    depId: 3,
    branchId: 2
  }, {
    depId: 3,
    branchId: 3,
    val: true
  }]
]

let data = {};
let j = 0;

permissions.forEach((row) => {
  let i = 0;
  let departmentSeen = false;

  row.forEach((col) => {
    if (col["val"] === true) {
      data[`data[${j}][branches][${i}]`] = col.branchId;
      data[`data[${j}][department]`] = col.depId;
      i++;
      departmentSeen = true;
    }
  });

  if (departmentSeen) {
    j++;
  }
});

console.log(data);

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

6 Comments

the data in this case should be "data[0][branches][0]": 1, "data[0][department]": 1, "data[0][branches][1]": 2, "data[1][branches][0]": 3, "data[1][department]": 3
the data index should be sequence
I am not sure what you mean by 'the data index should be sequence', can you elaborate?
like this (from the example result) "data[1][branches][0]": 3, instead of "data[2][branches][2]": 3, @IvanD
I edited the solution. Please note that output will have the different order of elements, but the elements are the same. Is it what you were trying to achieve?
|

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.