how to set a unique aws S3 file name? - angularjs

I'm sending my file to an s3 bucket through the front end, as that seems more efficient from what i've read.
But, for some of my schemas / collections, I will not have the id for which the file / photo is associated -- as they are being created the same time as the upload:
$scope.add = function(){
if($scope.file.photo){
$scope.distiller.photo = 'http://s3.amazonaws.com/whiskey-upload/distillers/'
+ ' needs to be assigned to guid or collection id'
Distillery.create($scope.distiller).then(function(res){
console.log(res);
$scope.distillers.push(res.data.distiller);
var files = $scope.file;
var filename = files.photo.$ngfName;
var type = files.type;
var folder = 'distillers/';
var query = {
files: files,
folder: folder,
filename: res.data.distiller._id,
type: type
};
Uploads.awsUpload(query).then(function(){
$scope.distiller = {};
$scope.file = {};
});
});
}
else{
Distillery.create($scope.distiller).then(function(res){
toastr.success('distillery created without photo');
$scope.distiller = {};
});
}
};
The above code wouldn't work, unless I sent an update on the aws.Upload promise after the distillery object was created and after the file was uploaded to s3.
That doesn't seem efficient.
I could create a guid and assign that to the s3 file name, and also keep a reference of that on the distillery object. This seems hacky, though.
example Guid creator:
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
What would be the cleanest way to achieve what I want?

A good GUID generator is a very standard way to solve a unique id problem. Depending on the algorithm, the chance of a name collision could be close to nil. As you know, JavaScript does not have a native one, so one like yours is reasonable. I don't think its hacky at all.
Here is another by #briguy37:
function generateUUID() {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid; };

Related

Sort JSON file with NODEJS

I just started using NodeJS (as of yesterday) I have managed to retrieve JSON data from an API as seen in the code below:
var request = require('request');
var fs = require('fs');
const pug = require('pug');
request.get('APICALL', function(err, data) {
if (err) { }
else {
console.log(data);
fs.writeFile('industries.txt', JSON.stringify(data));
}
});
The JSON data that my request.get returns can be found here JSON FILE
What I want to do is sort through this data, find out which revenue and industry ID appears the most (i.e for this data it's "revenue:8" and "industry:69") once this is done I want to return only companies who match both revenue 8 and industry 9.
Is this possible and what would be the best way to approach such a task?
You can count the revenue - industry_id occurences, sort descending and then filter by that:
let arr = [{"id":1,"domain":"google.com","alexa_rank":1,"country":236,"employees":"7","revenue":"8","industry_id":"69"},{"id":3,"domain":"youtube.com","alexa_rank":2,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":2,"domain":"facebook.com","alexa_rank":3,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":4,"domain":"yahoo.com","alexa_rank":6,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":111,"domain":"reddit.com","alexa_rank":9,"country":236,"employees":"3","revenue":"2","industry_id":"69"},{"id":9,"domain":"twitter.com","alexa_rank":12,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":230,"domain":"360.cn","alexa_rank":20,"country":46,"employees":"5","revenue":"8","industry_id":"69"},{"id":30,"domain":"weibo.com","alexa_rank":22,"country":46,"employees":"5","revenue":"2","industry_id":"69"},{"id":19,"domain":"google.de","alexa_rank":23,"country":236,"employees":"7","revenue":"8","industry_id":"65"},{"id":23,"domain":"google.co.uk","alexa_rank":24,"country":236,"employees":"7","revenue":"8","industry_id":"45"},{"id":12,"domain":"linkedin.com","alexa_rank":25,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":25,"domain":"google.fr","alexa_rank":26,"country":76,"employees":"1","revenue":"2","industry_id":"65"},{"id":46,"domain":"google.ru","alexa_rank":27,"country":236,"employees":"0","revenue":"8","industry_id":"69"},{"id":33,"domain":"google.com.br","alexa_rank":28,"country":32,"employees":"3","revenue":"3","industry_id":"47"},{"id":22,"domain":"yandex.ru","alexa_rank":29,"country":182,"employees":"5","revenue":"7","industry_id":"69"},{"id":20,"domain":"google.com.hk","alexa_rank":31,"country":46,"employees":"0","revenue":"8","industry_id":"69"},{"id":101,"domain":"netflix.com","alexa_rank":32,"country":236,"employees":"5","revenue":"8","industry_id":"38"},{"id":35,"domain":"google.it","alexa_rank":34,"country":236,"employees":"6","revenue":"8","industry_id":"113"},{"id":3351716,"domain":"ntd.tv","alexa_rank":36,"country":236,"employees":"4","revenue":"0","industry_id":"13"},{"id":77,"domain":"imgur.com","alexa_rank":37,"country":236,"employees":"3","revenue":"2","industry_id":"69"},{"id":21,"domain":"ebay.com","alexa_rank":38,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":41,"domain":"google.es","alexa_rank":39,"country":236,"employees":"1","revenue":"2","industry_id":"65"},{"id":74,"domain":"pornhub.com","alexa_rank":40,"country":58,"employees":"1","revenue":"3","industry_id":"38"},{"id":13,"domain":"msn.com","alexa_rank":41,"country":236,"employees":"8","revenue":"8","industry_id":"69"},{"id":18,"domain":"wordpress.com","alexa_rank":43,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":507,"domain":"aliexpress.com","alexa_rank":44,"country":46,"employees":"7","revenue":"8","industry_id":"69"},{"id":42,"domain":"livejasmin.com","alexa_rank":47,"country":128,"employees":"1","revenue":"0","industry_id":"38"},{"id":28,"domain":"microsoft.com","alexa_rank":48,"country":236,"employees":"8","revenue":"8","industry_id":"25"},{"id":58,"domain":"google.ca","alexa_rank":49,"country":236,"employees":"7","revenue":"8","industry_id":"69"},{"id":44,"domain":"tumblr.com","alexa_rank":50,"country":236,"employees":"4","revenue":"5","industry_id":"69"},{"id":107,"domain":"stackoverflow.com","alexa_rank":52,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":2553,"domain":"twitch.tv","alexa_rank":53,"country":236,"employees":"4","revenue":"4","industry_id":"69"},{"id":40,"domain":"imdb.com","alexa_rank":58,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":488,"domain":"github.com","alexa_rank":61,"country":236,"employees":"4","revenue":"5","industry_id":"25"},{"id":239,"domain":"pinterest.com","alexa_rank":62,"country":236,"employees":"4","revenue":"4","industry_id":"69"},{"id":1853,"domain":"popads.net","alexa_rank":65,"country":53,"employees":"1","revenue":"1","industry_id":"84"},{"id":51,"domain":"xvideos.com","alexa_rank":66,"country":59,"employees":"0","revenue":"0","industry_id":"38"},{"id":7154677,"domain":"diply.com","alexa_rank":68,"country":40,"employees":"3","revenue":"0","industry_id":"100"},{"id":245,"domain":"csdn.net","alexa_rank":69,"country":46,"employees":"3","revenue":"2","industry_id":"69"},{"id":189,"domain":"wikia.com","alexa_rank":72,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":275,"domain":"google.com.tw","alexa_rank":74,"country":218,"employees":"0","revenue":"8","industry_id":"95"},{"id":8456,"domain":"whatsapp.com","alexa_rank":75,"country":236,"employees":"3","revenue":"2","industry_id":"69"},{"id":56,"domain":"xhamster.com","alexa_rank":78,"country":58,"employees":"1","revenue":"0","industry_id":"38"},{"id":44705842,"domain":"txxx.com","alexa_rank":81,"country":0,"employees":"1","revenue":"0","industry_id":"38"},{"id":2793171,"domain":"coccoc.com","alexa_rank":84,"country":243,"employees":"3","revenue":"0","industry_id":"65"},{"id":1000482,"domain":"bongacams.com","alexa_rank":85,"country":58,"employees":"0","revenue":"0","industry_id":"38"},{"id":92,"domain":"google.pl","alexa_rank":86,"country":236,"employees":"6","revenue":"8","industry_id":"77"},{"id":794,"domain":"pixnet.net","alexa_rank":86,"country":218,"employees":"3","revenue":"0","industry_id":"69"},{"id":249,"domain":"dropbox.com","alexa_rank":87,"country":236,"employees":"5","revenue":"7","industry_id":"69"},{"id":62718286,"domain":"porn555.com","alexa_rank":90,"country":0,"employees":"0","revenue":"0","industry_id":"38"},{"id":141,"domain":"google.co.th","alexa_rank":91,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":1215,"domain":"gmw.cn","alexa_rank":92,"country":46,"employees":"0","revenue":"0","industry_id":"97"},{"id":130,"domain":"google.com.pk","alexa_rank":93,"country":236,"employees":"3","revenue":"2","industry_id":"84"},{"id":317,"domain":"china.com","alexa_rank":98,"country":46,"employees":"5","revenue":"8","industry_id":"100"},{"id":148727,"domain":"amazon.in","alexa_rank":100,"country":236,"employees":"8","revenue":"8","industry_id":"69"},{"id":115,"domain":"google.com.ar","alexa_rank":101,"country":11,"employees":"0","revenue":"8","industry_id":"69"},{"id":375,"domain":"soundcloud.com","alexa_rank":103,"country":83,"employees":"4","revenue":"3","industry_id":"69"},{"id":39,"domain":"fc2.com","alexa_rank":104,"country":236,"employees":"0","revenue":"0","industry_id":"69"},{"id":62729524,"domain":"clicksgear.com","alexa_rank":104,"country":0,"employees":"0","revenue":"0","industry_id":"84"},{"id":2711,"domain":"bbc.com","alexa_rank":107,"country":235,"employees":"6","revenue":"8","industry_id":"13"},{"id":109,"domain":"google.nl","alexa_rank":108,"country":155,"employees":"7","revenue":"8","industry_id":"65"},{"id":146,"domain":"adf.ly","alexa_rank":110,"country":235,"employees":"1","revenue":"2","industry_id":"65"},{"id":7739,"domain":"uptodown.com","alexa_rank":111,"country":209,"employees":"1","revenue":"2","industry_id":"69"},{"id":26665,"domain":"ettoday.net","alexa_rank":111,"country":46,"employees":"0","revenue":"0","industry_id":"100"},{"id":240,"domain":"booking.com","alexa_rank":112,"country":155,"employees":"6","revenue":"8","industry_id":"69"},{"id":95,"domain":"dailymotion.com","alexa_rank":113,"country":76,"employees":"4","revenue":"5","industry_id":"69"},{"id":1022,"domain":"quora.com","alexa_rank":115,"country":236,"employees":"3","revenue":"2","industry_id":"69"},{"id":48,"domain":"ask.com","alexa_rank":116,"country":236,"employees":"4","revenue":"5","industry_id":"69"},{"id":2353727,"domain":"espn.com","alexa_rank":117,"country":236,"employees":"5","revenue":"7","industry_id":"13"},{"id":7170357,"domain":"blastingnews.com","alexa_rank":119,"country":216,"employees":"4","revenue":"2","industry_id":"100"},{"id":86,"domain":"nytimes.com","alexa_rank":120,"country":236,"employees":"5","revenue":"8","industry_id":"97"},{"id":1000001,"domain":"blogger.com","alexa_rank":124,"country":236,"employees":"7","revenue":"8","industry_id":"69"},{"id":106,"domain":"vimeo.com","alexa_rank":125,"country":236,"employees":"4","revenue":"5","industry_id":"69"},{"id":10725,"domain":"savefrom.net","alexa_rank":125,"country":182,"employees":"1","revenue":"0","industry_id":"69"},{"id":28040741,"domain":"daikynguyenvn.com","alexa_rank":126,"country":236,"employees":"0","revenue":"0","industry_id":"100"},{"id":547,"domain":"detik.com","alexa_rank":127,"country":103,"employees":"4","revenue":"5","industry_id":"100"},{"id":1282,"domain":"stackexchange.com","alexa_rank":129,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":200,"domain":"google.co.ve","alexa_rank":131,"country":236,"employees":"0","revenue":"8","industry_id":"69"},{"id":157,"domain":"google.co.za","alexa_rank":132,"country":205,"employees":"2","revenue":"2","industry_id":"82"},{"id":221,"domain":"salesforce.com","alexa_rank":132,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":2394,"domain":"vice.com","alexa_rank":135,"country":236,"employees":"5","revenue":"8","industry_id":"118"},{"id":4637,"domain":"tribunnews.com","alexa_rank":136,"country":103,"employees":"3","revenue":"2","industry_id":"87"},{"id":88,"domain":"ebay.co.uk","alexa_rank":138,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":177,"domain":"slideshare.net","alexa_rank":140,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":1816926,"domain":"theguardian.com","alexa_rank":141,"country":235,"employees":"5","revenue":"6","industry_id":"97"},{"id":1594,"domain":"spotify.com","alexa_rank":143,"country":215,"employees":"5","revenue":"8","industry_id":"95"},{"id":113,"domain":"xnxx.com","alexa_rank":144,"country":59,"employees":"1","revenue":"0","industry_id":"38"},{"id":236,"domain":"google.com.vn","alexa_rank":145,"country":243,"employees":"0","revenue":"8","industry_id":"9"},{"id":44263656,"domain":"adexchangeprediction.com","alexa_rank":146,"country":0,"employees":"0","revenue":"0","industry_id":"84"},{"id":884,"domain":"buzzfeed.com","alexa_rank":147,"country":236,"employees":"5","revenue":"5","industry_id":"69"},{"id":4263,"domain":"chaturbate.com","alexa_rank":148,"country":236,"employees":"2","revenue":"1","industry_id":"38"},{"id":161,"domain":"nicovideo.jp","alexa_rank":149,"country":111,"employees":"4","revenue":"5","industry_id":"69"},{"id":225,"domain":"google.gr","alexa_rank":149,"country":86,"employees":"1","revenue":"1","industry_id":"35"},{"id":158,"domain":"chase.com","alexa_rank":151,"country":236,"employees":"8","revenue":"8","industry_id":"44"},{"id":148,"domain":"mozilla.org","alexa_rank":153,"country":236,"employees":"5","revenue":"6","industry_id":"69"},{"id":723,"domain":"avito.ru","alexa_rank":153,"country":182,"employees":"4","revenue":"4","industry_id":"69"},{"id":87,"domain":"cnet.com","alexa_rank":155,"country":236,"employees":"4","revenue":"5","industry_id":"100"},{"id":224,"domain":"google.com.co","alexa_rank":156,"country":49,"employees":"7","revenue":"2","industry_id":"47"},{"id":340,"domain":"indeed.com","alexa_rank":157,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":558,"domain":"flipkart.com","alexa_rank":157,"country":102,"employees":"6","revenue":"8","industry_id":"69"}]
let mostOccurence = [...arr.reduce((a,b) => {
a.set(b.revenue + ":" + b.industry_id, a.has(b.revenue + ":" + b.industry_id) ? a.get(b.revenue + ":" + b.industry_id) + 1 : 1);
return a;
}, new Map)].sort((a,b) => b[1] - a[1])[0][0];
let split = mostOccurence.split(':'), revToLook = split[0], indToLook = split[1];
let filtered = arr.filter(e => e.revenue === revToLook && e.industry_id === indToLook);
console.log(filtered);

Using Alasql can you replace null column with empty string

In my angular controller i have ExportToExcel function, which is using alasql to export data to Excel and the data is coming from angular array.
The issue is the array might contains null as data, is it a way to replace null to empty string in alasql
Following is the function
ac.ExportToExcel = function () {
var time = new Date().toJSON().slice(0, 10);
alasql('SELECT * INTO XLSX("ExchangeReport' + time + '.xlsx",{headers:true}) FROM ? WHERE BillingPeriod = "' + ac.ExchangeReport.BillingPeriod + '" or "' + ac.ExchangeReport.BillingPeriod + '" = "" ', [ac.ExchangeDatas]);
}
And this is my data source
ac.ExchangeDatas = [];
from
ac.GetAllExchangeData = function () {
AccountRepository.GetAllExchangeData().$promise.then(
function (data) {
ac.ExchangeDatas = data.result;
},
function (err) {
console.log("error in GetAllExchangeData : " + err);
}
);
}
Data in excel:
var old = JSON.stringify($scope.achData).replace(/null/g, '""'); //convert to
JSON string and puts empty string in place of null values
var newArray = JSON.parse(old); //convert back to array
alasql('select * into XLSX("amar.xlsx", {headers:true}) from ?', [newArray]);
You should be able to use COALESCE, though you need to list all the columns like COALESCE(YourColumn,'') AS YourColumn instead of *. See https://github.com/agershun/alasql/wiki/Coalesce

serialize a client-side made string to a server-side file angularJS

So i'm building a string which is actually an xml file. Since i can't save any file from client-side for security issues, i'd like to save this string in a xml file server-side.
i've read about $http method or upload plugin but it seems a bit a complicated for just sending a string to my server.
is there an easier way to do it?
For informative purpose, here is my code :
$scope.addBlock = function(){
if($scope.parentblock == null)//creation of a lvl1 element
{
var div = document.createElement("div");
//xml creation
var elmt = document.createElement($scope.selectedtype.tagname);
//
div.setAttribute('id',$scope.blocktitle);
div.setAttribute('lvl',0);
var path = '/'+$scope.selectedtype.tagname;
div.setAttribute('path',path);
div.innerHTML = '<h1>' + $scope.blocktitle + '</h1><p> path : '+ path + '</p>';
if($scope.blockvalue)
{
div.innerHTML = div.innerHTML + '<p>' + $scope.blockvalue + '</p>';
//adding value to xml tag
elmt.innerHTML = $scope.blockvalue;
//
}
document.getElementById('blocks').appendChild(div);
//xml adding
doc.appendChild(elmt);
console.log(doc);
//
$scope.blocks.push([$scope.blocktitle,$scope.selectedtype.tagname,$scope.parentblock,path]);
}
else //creation of a lvl n element
{
var div = document.createElement("div");
//xml creation
var elmt = document.createElement($scope.selectedtype.tagname);
//
div.setAttribute('id',$scope.blocktitle);
var lvl = Number(document.getElementById($scope.parentblock[0]).getAttribute('lvl'))+1;
div.setAttribute('lvl',lvl);
var path = ($scope.parentblock[3]+'/'+$scope.selectedtype.tagname);
div.innerHTML = '<h2>' + (new Array(lvl + 1).join("&nbsp")) + $scope.blocktitle +
'</h2><p>' + (new Array(lvl + 5).join("&nbsp")) + ' path : '+ path + '</p>';
if($scope.blockvalue)
{
div.innerHTML = div.innerHTML + '<p>' + (new Array(lvl + 5).join("&nbsp")) +
$scope.blockvalue + '</p>';
//adding value to xml tag
elmt.innerHTML = $scope.blockvalue;
//
}
document.getElementById($scope.parentblock[0]).appendChild(div);
//xml adding
doc.getElementsByTagName($scope.parentblock[1].toLowerCase())[0].appendChild(elmt);
//
$scope.blocks.push([$scope.blocktitle,$scope.selectedtype.tagname,$scope.parentblock,path]);
}
//console.log(doc);
}
I need to send doc to my server.
EDIT :
i really need to send an xml since is a iso19110-normalized file that i have to save.
I tried this :
$http({
method: 'POST',
url: 'save.php',
data: { 'doc' : doc.outerHTML },
headers: {'Content-Type': 'text/xml'}
})
.success(function(data){
alert(data);
})
.error(function(){
alert('fail');
});
and this in php :
file_put_contents('test.xml', $_POST['doc'])
but i've got "index doc undefined" ...
I also tried
$http.post('save.php',doc).success(function() {
return console.log('uploaded');
});
but i got 'Converting circular structure to JSON'
So i think my problem come from the location of the data. I can't figure where it is...
If you need to send a lump of text to your server (over http) then you should use the $http.post() method. Its easy to use and works like a charm. You can receive and save the data on the server in any number of ways, presumably you have this bit worked out?
The real issue from your question, imo, is whether you really need to be sending xml? A much easier way of sending your data would be to use JSON. Then you don't need to go through all that document building rigmarole. Instead you can just build a javascript object and send it using the $http.post() method letting angular do all the work for you.
var doc;
doc = {
id: $scope.blocktitle,
lvl: 0
// rest of your doc structure here
};
$http.post('/my-url/upload/doc', doc).success(function() {
return console.log('uploaded');
});

node.js loop through array to write files

I've been working through a few others, also this of looping through array to http.get data from a variety of sources. I understand that nodeJS is working asynchronously which is allowing the files to be written empty or with incomplete data, but I can't seem to get past this point.
Problem: calls are made, files are built but the files are always empty
Goal: loop through an array to create files locally from the sites data. Here is what I've got so far:
var file_url = 'http://js.arcgis.com/3.8amd/js/esri/',
DOWNLOAD_DIR = './esri/',
esriAMD = [ '_coremap.js', 'arcgis/csv.js'];
function readFile(callback) {
if (esriAMD.length > 0) {
var setFile = esriAMD.shift(),
file_name = url.parse(file_url).pathname.split('/').pop(),
trial = setFile.split('/').pop(),
file = fs.createWriteStream(DOWNLOAD_DIR + trial);
http.get(file_url + esriAMD, function(res) {
res.on('data', function(data) {
file.write(data);
console.log(setFile + ' has been written successfully');
});
res.on('end', function(){
console.log(setFile + ' written, moving on');
console.log(esriAMD.length);
readFile(callback);
});
//readFile(callback);
});
} else {
callback();
}
}
readFile(function() {
console.log("reading finishes");
});
Any insight would really help.
thanks,
var esriAMD = [....];
...
function readFile(callback) {
...
http.get(file_url + esriAMD, function(res) {
...
concatenating strings with arrays may yield unexpected results.
you want to make sure that
you know what URLs your program is accessing
your program deals with error situations (where the fsck is res.on('error', ...)?)
Solution: I was passing the wrong variable into the http.get
Working code:
var file_url = 'http://.....',
DOWNLOAD_DIR = './location/';
esriAMD = ['one', 'two', 'three'..0;
function readFile(callback) {
if(esriAMD.length > 0) {
var setFile = esriAMD.shift(),
file_name = url.parse(setFile).pathname.split('/').pop(),
trial = setFile.split('/').pop(),
file = fs.createWriteStream(DOWNLOAD_DIR + trial);
http.get(file_url + setFile, function(res){
res.on('error', function(err){
console.log(err);
});
res.on('data', function(data){
file.write(data);
console.log(setFile + ' started');
});
res.on('end', function(){
console.log(setFile + ' completed, moving on');
});
});
} else {
callback();
}
}

Why Array.length does not work with key string

I've been reading and doing some testing and found that the command. "Length" of the Array () javascript does not work when the array's keys are string. I found that to run this command, I use whole keys.
However, I wonder if any of you can tell me why this rule? Limitation of language, or specification of logic? Or, my mistake ...?
Thanks
Obs.: The key to my array is a string (name of component) but the values and the array of objects.
Declarating:
objElementos = new Array();
Object:
objElemento = function(Id){
this.Id = $('#' + Id).attr('id');
this.Name = $('#' + Id).attr('name');
this.CssLeft = $('#' + Id).css('left');
this.CssBottom = $('#' + Id).css('bottom');
this.Parent = $('#' + Id).parent().get(0);
this.Childs = new Array();
this.addChild = function(IdObjChild) {
try {
if ($('#' + IdObjChild).attr('id') != '')
this.Childs[$('#' + IdObjChild).attr('id')] = new objElemento(IdObjChild);
} catch(err){ $('#divErrors').prepend('<p>' + err + '</p>'); }
}
this.getChildCount = function(){
try {
$('#divErrors').prepend('<p>' + this.Childs.length + '</p>');
return this.Childs.length;
} catch(err){ $('#divErrors').prepend('<p>' + err + '</p>'); }
}
this.updateCssLeft = function(CssPosition){
try {
$('#divErrors').prepend('<p>updateCssLeft:' + this.Id + ' / ' + this.CssLeft + '</p>');
$('#' + this.Id).css('left',CssPosition);
this.CssLeft = $('#' + this.Id).css('left');
$('#divErrors').prepend('<p>updateCssLeft:' + this.Id + ' / ' + this.CssLeft + '</p>');
} catch(err){ $('#divErrors').prepend('<p>' + err + '</p>'); }
}
this.updateCssBottom = function(CssPosition){
try {
$('#divErrors').prepend('<p>updateCssBottom:' + this.Id + ' / ' + this.CssBottom + '</p>');
$('#' + this.Id).css('bottom',CssPosition);
this.CssBottom = $('#' + this.Id).css('bottom');
$('#divErrors').prepend('<p>updateCssBottom:' + this.Id + ' / ' + this.CssBottom + '</p>');
} catch(err){ $('#divErrors').prepend('<p>' + err + '</p>'); }
}
}
Use:
objElementos['snaptarget'] = new objElemento('snaptarget');
When using string "indexers" on an array, you are effectively treating it as an object and not an array, and tacking extra fields onto that object. If you are not using integer indexes, then there's not really much point in using the Array type and it makes more sense to use the Object type instead. You could count the fields as follows:
var c=0;
for(var fieldName in obj)
{
c++;
}
Since arrays with key strings are objects, you can use:
Object.keys(objElementos).length
The reason is because when you are using strings as your keys, you're really declaring an object, rather than an array. As such, there is no length attribute for objects.

Resources