Javascript transform an array created from inputs checkbox

0

I have multiple input checkbox created from a model with angular and ng-repeat, when I select some of it I get this, for example:

var accounts: [{'AA764':true}, {'AA324': true}, {'AA234': false}, {'AA553': true}, {'AA7365': false}];

But the structure that I need to manipulate in the controller where I call a REST API service is like this:

var accounts: ['AA764', 'AA324', 'AA553'];

Only by bringing the key of those that are selected, I have tried with the function of javascript foreach but I can not make it work. Is there any Angular service or external library that can help me with this transformation? Thanks.

    
asked by AndreFontaine 02.03.2016 в 04:42
source

3 answers

5

You could use something like this

var accounts= [{'AA764':true}, {'AA324': true}, {'AA234': false}, {'AA553': true}, {'AA7365': false}];

var result = _.map(accounts, function(item, index){
	return Object.getOwnPropertyNames(item);
});

console.log(result.join(","));
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

To implement the iteration between the items in the list I like to use the functionality of the library

underscorejs

although this is optional you could iterate otherwise if you think it is convenient.

Although the trick is in the use of Object.getOwnPropertyNames ()

    
answered by 02.03.2016 / 05:07
source
0

The issue is that you have an array of objects where the value of your <input type="checkbox" /> is a key and the value is whether the object is marked or not. This becomes a problem because as it is an object you can not access its properties as if it were an array. To do this, you must use artifices like the one shown below.

Using pure JavaScript and the functional paradigm:

var accounts = [{'AA764':true}, {'AA324': true}, {'AA234': false}, {'AA553': true}, {'AA7365': false}];
var validKeys = accounts
    .filter(x => { var first; for (first in x) break; return x[first]; } )
    .map(x => { var first; for (first in x) break; return first; } );
alert(validKeys);

If you use browsers that support JavaScript JS 1.8.5 or higher, the code is reduced to:

var accounts = [{'AA764':true}, {'AA324': true}, {'AA234': false}, {'AA553': true}, {'AA7365': false}];
var validKeys = accounts
    .filter(x => x[Object.keys(x)[0]])
    .map(x => Object.keys(x)[0] );
alert(validKeys);

Response based on Javascript: Getting the first index of an object

    
answered by 02.03.2016 в 06:05
0

You can use the native functions of the fixes in javascript ( filter and map ) to filter the selected ones first and then obtain only the identifier.

// Tu colección
var accounts = [{'AA764':true}, {'AA324': true}, {'AA234': false}, {'AA553': true}, {'AA7365': false}];

// Se obtienen solo los elementos con valor true
var selected = accounts.filter(function(item){
    return item[Object.getOwnPropertyNames(item)];
});

// Se obtiene el arreglo únicamente con los identificadores
var selectedIDs = account.map(function(item){
    return Object.getOwnPropertyNames(item);
});
    
answered by 03.03.2016 в 15:29