Exchange array data between 'S' and 'N'

2

I have a function that I will adapt for a button, which will exchange a data within an array, between S and N .

The code I have is:

var array = [{"nome":"1","faltou":"N","goleiro":"N","pago":"N"},{"nome":"2","faltou":"N","goleiro":"N","pago":"N"},{"nome":"3","faltou":"N","goleiro":"N","pago":"N"}];


function toggleArrayItem(a, v, d) {
        var i = a.findIndex(function(val){
                return val.nome === v;
            });
    if (i != -1){
      var data = a[i];
            if(d === 'faltou'){
        const isOn = (data.faltou = 'N');
        a[i] = ({"nome":data.nome, "faltou":isOn ? 'N' : 'S', "goleiro":data.goleiro, "pagou":data.pagou});
      } else if(d === 'goleiro'){
        const isOn = data.goleiro = 'N';
        a[i] = ({"nome":data.nome, "faltou":data.faltou, "goleiro":isOn ? 'N' : 'S', "pagou": data.pagou});
      } else if(d === 'pagou'){
        const isOn = data.pagou = 'N';
        a[i] = ({"nome":data.nome, "faltou":data.faltou, "goleiro":data.goleiro, "pagou": isOn ? 'N' : 'S'});
      }
    }
}

toggleArrayItem(array, '1', 'faltou');.

It just is not working properly, and I wanted to lower the code. I'm using localStorage in JSON.

How to proceed?

    
asked by anonymous 18.10.2018 / 23:44

2 answers

2

You have minor typing and logic errors that make your code not work:

  • const isOn = (data.faltou = 'N'); - Notice that the comparison failed in the right part of the expression, and what you wanted to do was data.faltou == 'N' or even === as you already have in some places. In addition, the value itself is inverted because isOn indicates if it is on or if it has S but in your case it is pointing to N .

  • , "pagou":data.pagou - here puts the value of data.pagou that was not defined in the original array and so will get undefined . In the original array was defined pago

Correcting these details already gives you what you expect:

var array = [{
  "nome": "1",
  "faltou": "N",
  "goleiro": "N",
  "pago": "N"
}, {
  "nome": "2",
  "faltou": "N",
  "goleiro": "N",
  "pago": "N"
}, {
  "nome": "3",
  "faltou": "N",
  "goleiro": "N",
  "pago": "N"
}];


function toggleArrayItem(a, v, d) {
  var i = a.findIndex(function(val) {
    return val.nome === v;
  });

  if (i != -1) {
    var data = a[i];
    if (d === 'faltou') {
      const isOn = (data.faltou === 'S');
      a[i] = ({
        "nome": data.nome,
        "faltou": isOn ? 'N' : 'S',
        "goleiro": data.goleiro,
        "pagou": data.pago
      });
    } else if (d === 'goleiro') {
      const isOn = data.goleiro === 'S';
      a[i] = ({
        "nome": data.nome,
        "faltou": data.faltou,
        "goleiro": isOn ? 'N' : 'S',
        "pagou": data.pago
      });
    } else if (d === 'pagou') {
      const isOn = data.pagou === 'N';
      a[i] = ({
        "nome": data.nome,
        "faltou": data.faltou,
        "goleiro": data.goleiro,
        "pagou": isOn ? 'N' : 'S'
      });
    }
  }
}

toggleArrayItem(array, '1', 'faltou');

console.log(array);

For simplification, you can start by using a Arrow Function in findIndex . Then to change the desired property you do not need to create a new equal object with only a different property. You can directly access the property you have and change, as long as you make sure that it is part of the properties you want to allow to change:

var array = [{
  "nome": "1",
  "faltou": "N",
  "goleiro": "N",
  "pago": "N"
}, {
  "nome": "2",
  "faltou": "N",
  "goleiro": "N",
  "pago": "N"
}, {
  "nome": "3",
  "faltou": "N",
  "goleiro": "N",
  "pago": "N"
}];


function toggleArrayItem(a, v, d) {
  var i = a.findIndex(val => val.nome === v);  
  if (i != -1 && ['faltou', 'goleiro', 'pagou'].includes(d)) {
    a[i][d] = a[i][d] === 'S' ? 'N' : 'S';
  }
}

toggleArrayItem(array, '1', 'faltou');

console.log(array);

As a final note I suggest better names for the variables. I left the names the ones I had to be most related to your original code but a , v and d are not good names and make the code complicated to read

    
19.10.2018 / 00:22
1

Hello, I have another suggestion, see:

 var array = [{
        "nome": "1",
        "faltou": "N",
        "goleiro": "N",
        "pago": "N"
    },
    {
        "nome": "2",
        "faltou": "N",
        "goleiro": "N",
        "pago": "N"
    },
    {
        "nome": "3",
        "faltou": "N",
        "goleiro": "N",
        "pago": "N"
    }];


    function toggleArrayItem(a, v, d) {

        a.forEach(item => {
            if (item.nome == v) {
                for (var prop in item) {
                    if (prop == d) {
                        item[prop]=item[prop] == 'S' ? 'N' : 'S';
                    }
                }
            }
        });

        console.log(a);
    }

    toggleArrayItem(array, '1', 'faltou');
    
19.10.2018 / 16:21