i trying push items different arrays. @ moment can using if statements, , if have many arrays? how can use dynamic variables push elements array?
use object references arrays (or original container create them in).
var env = { // var = [0, 2, 4], b = [1, 3, 5], 'foo': [0, 2, 4], // env = {}; 'bar': [1, 3, 5] // env['foo'] = a; }; // env['bar'] = b; function addtoarr(k, x) { // `k` string key, `x` item append env[k].push(x); // push `x` array ref `env[k]` } addtoarr('foo', 6); // example usage addtoarr('bar', 7); env; // result /* { "foo": [0, 2, 4, 6], // === "bar": [1, 3, 5, 7] // === b } */
of course, can use whatever logic object's keys , how function decides upon use.
Comments
Post a Comment