Looping over objects in array and adding property to each - javascript

I'm getting an array of files, and then I want to add the date and size properties to each of those file objects, but using the code below, they don't get added. I know that's my fs.statSync(p + file).mtime.getTime() and fs.statSync(p + file).size have values in them.
var files = fs.readdirSync(p);
files.sort(function(a, b) {
return fs.statSync(p + a).mtime.getTime() -
fs.statSync(p + b).mtime.getTime();
});
files.forEach(function(file) {
file.date = fs.statSync(p + file).mtime.getTime();
file.size = fs.statSync(p + file).size;
});
console.log('files::'+files); // doesn' have the new file.date and file.size property.

When you writing value into file variable it's not saving because file it's variable that lives into local scope. So a quick solution for this:
var files = fs.readdirSync(p),
result = [];
files.sort(function(a, b) {
return fs.statSync(p + a).mtime.getTime() -
fs.statSync(p + b).mtime.getTime();
});
files.forEach(function(file) {
file.date = fs.statSync(p + file).mtime.getTime();
file.size = fs.statSync(p + file).size;
result.push(file);
});
console.log('files::' + result);

Similar to Eugene's answer, but this uses map:
files = files.map(function(file) {
file.date = fs.statSync(p + file).mtime.getTime();
file.size = fs.statSync(p + file).size;
return file;
});
DEMO

The file variable is a local variable. Without having to create a new array as Eugene did, you can update the original array in this way:
var files = fs.readdirSync(p);
files.sort(function(a, b) {
return fs.statSync(p + a).mtime.getTime() -
fs.statSync(p + b).mtime.getTime();
});
files.forEach(function(file, index, array) {
array[index].date = fs.statSync(p + file).mtime.getTime();
array[index].size = fs.statSync(p + file).size;
});
console.log('files::'+files);

Related

Creating directory after each event

I am trying to create a directory after each button click event.
This works just until 10 directories
5612cfea107f9e0f356b3dee_1
5612cfea107f9e0f356b3dee_2
5612cfea107f9e0f356b3dee_3
5612cfea107f9e0f356b3dee_n
and then I am getting this error:
Error: EEXIST: file already exists, mkdir 'user/public/uploadGallery/5612cfea107f9e0f356b3dee_10'
app.post('/createDirectories', function(req, res) {
var id = '5612cfea107f9e0f356b3dee';
var pathDirectory = __dirname + '/public/uploadGallery/' + id;
fs.readdir(__dirname + '/public/uploadGallery/', function (err, files) {
var countVal = files.filter(junk.not).length;
var fileVal = files.filter(junk.not);
if(countVal == '0'){
fs.mkdirSync(pathDirectory + '_' + 1);
console.log("Directory created: " + pathDirectory + '_' + 1);
}else{
var lastElem = fileVal[fileVal.length-1];
var lastElemSplitValue = lastElem.split("_")[1];
var valInt = parseInt(lastElemSplitValue, 10) +1;
fs.mkdirSync(pathDirectory + '_' + valInt);
}
});
});
What can I do to fix this problem? I wanna create n directories.
Thanks for your help.
machu
The problem is sorting
you'll have directories
_1
_2
...
_9
add the 10th - and, in alphabetical or lexical order, you'll have
_1
_10
_2
...
_9
so, the last folder is _9 ... 9 + 1 = 10 ... that already exists!
You could change your code to
} else {
var valInt = Math.max.apply(null, fileVal.map(function(entry) {
return parseInt(entry.split("_").pop(), 10);
})) + 1;
fs.mkdirSync(pathDirectory + '_' + valInt);
}
This applies Math.max to the result of mapping the fileVal entries to the parseInt of the last part of each of the fileVal entries split by '_'

JavaScript foreach Key Value array

I have just this array :
var sArray = {856:"users", 857:"avatars", 858:"emails"};
and I want to use forEach in a way to get key and value from that:
key = 856
value = user
My $.each code doesn't return the result I'm expecting, and I get instead:
856:user
I must be separate that with : to get key and value from this array.
My code is:
$.each(template_array, function(key, value) {
console.log("key: " + "value: " + value);
});
How to access key and value without separate?
Just take Object.keys for the keys and Array.prototype.forEach for the values in plain Javascript.
var sArray = { 856: 'users', 857: 'avatars', 858: 'emails'};
Object.keys(sArray).forEach(function (key) {
document.write('key: ' + key + ', value: ' + sArray[key] + '<br>');
});
Just concatenate them using +
var template_array = {
856: 'users',
857: 'avatars',
858: 'emails'
};
$.each(template_array, function(key, value) {
console.log('key:' + key + ', value:' + value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
UPDATE :
I think you have an array then use the following code
var template_array = ['856: users',
'857: avatars',
'858: emails'
];
template_array.forEach(function(v) {
v = v.split(':');
console.log('key:' + v[0] + ', value:' + v[1]);
});
try this one
var template_array = {
856: 'users',
857: 'avatars',
858: 'emails'
};
var date = [];
$.each(template_array,function (key , val){
date.push({key:key, value:val})
});
console.log(date)
var template_array = {
856: 'users',
857: 'avatars',
858: 'emails'
};
for (key in template_array) {
console.log('key:' + key + ', value:' + template_array[key]);
});

how to run javascript object from string without using eval()

i have a json object that has item type and id, i need to create new object
var data = {
"items":[
{"type":"generator","id":"item_1","x":200,"y":200},
{"type":"battery","id":"item_2","x":50,"y":300},
{"type":"generator","id":"item_3","x":200,"y":280},
{"type":"battery","id":"item_4","x":100,"y":400}
]
};
and i need to run for each item in items
jQuery.each(data.items, function(index,value) {
eval("var " + value.id + " = new " + value.type + "(" + (index + 1) + ");");
eval(value.id + ".id = '" + value.id + "';");
eval(value.id + ".draw(" + value.x + "," + value.y + ");")
});
this is not a good practice, but what else can i do?
i need then to have the control on the items
something like
item_1.moveto(300,700);
but i always get item_1 is undefind
You can create a factory method which allows to generate concrete types out of an abstract data structure:
var createItem = (function () {
var types = {};
function createItem(index, data) {
data = data || {};
var ctor = types[data.type], item;
if (!ctor) throw new Error("'" + data.type + "' is not a registered item type.");
item = new ctor(index);
item.id = data.id;
return item;
}
createItem.registerType = function (type, ctor) {
types[type] = ctor;
};
return createItem;
})();
Then register item types to the factory:
function Generator(index) {/*...*/}
createItem.registerType('generator', Generator);
And finally create an object map to lookup your items by id (you could use a specialized object like ItemsMap instead of a plain object), loop through your items and add them to the map.
var itemsMap = {};
data.items.forEach(function (itemData, i) {
var item = itemsMap[itemData.id] = createItem(i + 1, itemData);
//you can also draw them at this point
item.draw(itemData.x, itemData.y);
});
You can now lookup objects by id like:
var item1 = itemsMap['item_1'];
var objects = {};
objects[value.id] = new window[value.type](index + 1);

Javascript seemingly existing object

Unfortunately I can't easily paste the whole script that generates the variable, but I don't see it would be relevant either. Please instruct for more details, if necessary.
Javascript shows this:
console.log(gl.boxes);
shows:
[{element:{0:{jQuery19104057279333682955:9}, context:{jQuery19104057279333682955:9}, length:1}, boxName:"testi1", boxX:1, boxY:"180"}]
so gl.boxes[0] should exist, right? Still...
console.log(gl.boxes[0])
shows: undefined.
So what can I be missing here?
EDIT:
I will paste some more code about the generation of gl.boxes. Should be mostly about creating the variable as array first:
gl.boxes = [];
Then there is a function that handles creating and pushing new objects:
this.addBox = function (box) {
var retBox = {};
retBox.element = $(document.createElement('div'));
retBox.boxName = box.boxName;
retBox.boxX = box.boxX ? box.boxX : rootParent.defaultX;
retBox.boxY = box.boxY ? box.boxY : rootParent.defaultY;
retBox.element
.html(retBox.boxName)
.addClass(rootParent.boxClass)
.offset({ left: retBox.boxX, top: retBox.boxY })
.draggable({
stop: gl.URLs.dragStopDiv(retBox)
});
retBox.element.appendTo(rootParent.containerDiv);
gl.boxes.push(retBox);
return retBox;
};
The objects are created based on URL. Ie. in this test I have inline JS:
gl.objects.addBox({"boxName":"testi1","boxX":"50","boxY":"180"});
Only other place where the gl.boxes is being used is generating a URL based on the objects:
for(key in gl.boxes) {
var position = gl.boxes[key].element.position();
uri +=
"boxName["+key+"]="+gl.boxes[key].boxName+"&"+
"boxX["+key+"]="+position.left+"&"+
"boxY["+key+"]="+position.top+"&";
}
Maybe you need to change your loop to use indexes:
var i = 0,
position = {};
for (i = 0; i < gl.box.length; i += 1) {
position = gl.boxes[i].element.position();
uri += "boxName[" + i + "]=" + gl.boxes[i].boxName + "&" + "boxX[" + i + "]=" + position.left + "&" + "boxY[" + i + "]=" + position.top + "&";
}

concatenating strings in .each loop

This may be a dumb question but I can't seem to get it or find it anywhere.
I have a .each function that returns the id's of a bunch of divs on a page and then assigns them a number. I need them to be outputted in a specific format all as one string so I can pass them to a database and use them as "sort_order" values. (I split them through a sproc in my database).
Here is my code:
jQuery('#updateAllContentButton').click(function() {
var count = 1;
jQuery('.CommentaryItem').each(function() {
var id = GetID(jQuery(this));
var data_str = (id + ':' + count + '~');
console.log(data_str);
count++;
});
});
So that returns each data_str on a separate line but I need it to return it like this:
144:2~145:3~146:4~147:4~148:5 (so on)
Any help would be appreciated, thanks!
Use map and join :
jQuery('#updateAllContentButton').click(function() {
console.log(jQuery('.CommentaryItem').map(function(i) {
return GetID(jQuery(this)) + ':' + (i+1);
}).get().join('~'));
});
Note that you don't have to count yourself : Most jQuery iteration functions do it for you.
You can use an array for that. Like this...
jQuery('#updateAllContentButton').click(function() {
var count = 1;
var arr = [];
jQuery('.CommentaryItem').each(function() {
var id = GetID(jQuery(this));
var data_str = (id + ':' + count + '~');
arr.push(data_str);
//console.log(data_str);
count++;
});
console.log(arr.join());
});
try this:
jQuery('#updateAllContentButton').click(function() {
var count = 1;
var data_str = "";
jQuery('.CommentaryItem').each(function() {
var id = GetID(jQuery(this));
data_str += (id + ':' + count + '~');
console.log(data_str);
count++;
});
});
change
var data_str = (id + ':' + count + '~');
to
data_str+ = (id + ':' + count + '~');
full code
jQuery('#updateAllContentButton').click(function() {
var count = 1,
data_str;
jQuery('.CommentaryItem').each(function() {
var id = GetID(jQuery(this));
data_str += (id + ':' + count + '~');
console.log(data_str);
count++;
});
});

Categories

Resources