Equivalent to Javascript's PHP list ()

1

Is there a JavaScript function equivalent to list() of PHP?

Suppose this JSON:

{  
   "ALERTA":[  
      {  
         "TITULO":"Erro!",
         "MENSAGEM":"Seu nome parece estar incorreto"
      },
      {  
         "TITULO":"Erro!",
         "MENSAGEM":"Seu nome parece estar incorreto"
      }
   ]
}

In PHP there is the possibility of using list next to foreach , in order to convert an index, from an array, to a variable .

foreach($json['ALERTA'] as list($titulo, $mensagem)){    
   // $titulo será "Erro!"
   // $mensagem será "Seu nome parece estar incorreto"
}

This means that you do not have to use the indexes $variavel['TITULO'] and $variavel[MENSAGEM] , instead I use only $titulo and $mensagem .

In Javascript / JQuery I only know (and use) of this method:

$.each(json['ALERTA'], function (nome, data) {
   // data['TITULO'] será "Erro!"
   // data['MENSAGEM'] será "Seu nome parece estar incorreto"
});

But I wanted to ELIMINATE the use of indexes ['TITULO'] and ['MENSAGEM'] , only for aesthetic questions .

I want a result close to this:

$.each(json['ALERTA'], function (nome, list(titulo, mensagem)) {
   // titulo ser "Erro!"
   // mensagem ser "Seu nome parece estar incorreto"
});

That way, as in PHP, it would not use the index. Is there any equivalent function list() of PHP in Javascript, what would it be? If not, is there another solution to eliminate the use of indexes in this case (without being a new loop)?

    
asked by anonymous 06.08.2016 / 09:55

1 answer

4

There is a way to do this using modern JavaScript (ES6). I talked about this technique Destructuring assignment here, in this answer . You can also read more about this at the MDN: Destructuring assignment .

If you combine this technique with the (also new) for of you can assign the values of these properties on the fly like this:

for (let { TITULO, MENSAGEM } of json.ALERTA) {
    console.log(TITULO, MENSAGEM);
}

Example: link

If you need support for legacy browsers you can convert this to Babel . You create the code you need in ES6 and then convert. In this case the example code would look like this:

"use strict";

var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
    for (var _iterator = json.ALERTA[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
        var _step$value = _step.value;
        var TITULO = _step$value.TITULO;
        var MENSAGEM = _step$value.MENSAGEM;

        console.log(TITULO, MENSAGEM);
    }
} catch (err) {
    _didIteratorError = true;
    _iteratorError = err;
} finally {
    try {
        if (!_iteratorNormalCompletion && _iterator.return) {
            _iterator.return();
        }
    } finally {
        if (_didIteratorError) {
            throw _iteratorError;
        }
    }
}

To do this with old-fashioned code you can do this:

Array.prototype.lista = function() {
    var args = [].slice.call(arguments);
    var cb = args.pop();
    this.forEach(function(el) {
        var params = args.map(function(prop) {
            return el[prop];
        });
        cb.apply(this, params);
    });
}

and then to use:

json.ALERTA.lista('TITULO', 'MENSAGEM', function(tit, msg) {
    console.log(tit, msg);
});

jsFiddle: link

    
06.08.2016 / 11:34