Exchange array data between’S' and 'N'

Asked

Viewed 58 times

2

I have a function that I will adapt to 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');.

Only it is not working properly, and wanted to decrease the code. I am using localStorage in JSON.

How to proceed?

2 answers

2


You have small errors of writing and logic that make your code not work:

  • const isOn = (data.faltou = 'N'); - Notice that you missed the comparison in the right part of the expression, and what you wanted to do was data.faltou == 'N' or even === as it already has in some places. Moreover the value itself is reversed because isOn indicates whether it is connected or whether it has S but in your case it’s pointing to the N.

  • , "pagou":data.pagou - here puts the value of data.pagou that was not set in the original array and so will catch undefined. In the original array has been 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);

As for simplification, you can start by using a Arrow Function in the findIndex. Then to exchange the desired property you do not need to create a new object equal only with a different property. You can access directly from the property you have and change, as long as you ensure 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 equal to the ones I had to be more relatable with your original code but a, v and d are not good names and make the code difficult to read

  • Thank you for the @Isac reply. I’m almost sure that this Arrow Function does not work properly in hybrid apps on Android 4.4 and 5, but I will test and already give an opinion.

  • 1

    @Guilhermelirio No problem. If it doesn’t work just put the findIndex as it had originally. But I already say that it is unlikely that support findIndex and not Arrow Functions. If you look at the caniuse with. and even on MDN sees that the support is very high leaving out only the classic IE.

  • I will test. In HTML it worked perfectly. Thanks for the help!

  • i am the type of person who likes to understand the code: could explain to me what does . includes(d) in your code?

  • 1

    @Guilhermelirio Understanding the whole code is always ideal. o includes is a array method that tests whether the array contains an element. It is a way to avoid having to do if (d === 'faltou' || d == 'goleiro' || d === 'pagou'), which usually gets longer but in your case it didn’t get as long because the variable d has a very short name.

1

Hello, I have another suggestion, see if it answers:

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

  • Thank you for your contribution @Adjaircosta! I’ll give you an understanding of the code as well!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.