How to push values from array into row in exceljs?
i try with this:
const columns = ['string', 'string2', 'string3'];
for(let i=0; i < columns.length; i++) {
workSheet.getRow(1).values = [dto.columns[i]];
}
but this code add only one item from the columns list :/
have anybody any idea how to resolve this problem? I must add objects from array, I know that, i can do this:
workSheet.getRow(1).values = ['string','string2', 'string3'];
but this is not for me :(
thanks for any help
You are reassigning the value inside the for loop. So, you will always get the last item inside the values property. You can initialize the values as empty array before for loop and then try putting the item in the values.
const columns = ['string', 'string2', 'string3'];
workSheet.getRow(1).values = [];
for(let i=0; i < columns.length; i++) {
workSheet.getRow(1).values.push(dto.columns[i]);
}
Related
Probably this is an obvius question, but I'm pretty new on JS.
This is always inserting the same item repeated, it is changing the value of the last item inserted on the array when the object change the value, how can I avoid it and insert all the values that I¡m itereating?
self.user.userSociedadesAreasLink =[];
var userSociedadesAreasLink = {};
for(var i =0 ; i< self.selectedSociedades.length ; i++){
userSociedadesAreasLink.sociedad = self.selectedSociedades[i];
self.user.userSociedadesAreasLink.push(userSociedadesAreasLink);
}
You are using the same object every time to push into the array, only changing the property's value. You have to create a new object everytime to make it as a unique object.
self.user.userSociedadesAreasLink =[];
for(var i =0 ; i< self.selectedSociedades.length ; i++){
var userSociedadesAreasLink = {};
userSociedadesAreasLink.sociedad = self.selectedSociedades[i];
self.user.userSociedadesAreasLink.push(userSociedadesAreasLink);
}
This should solve the issue.
You could just push a new object literal each time. You could also just use a map operation for this.
self.user.userSociedadesAreasLink = self.selectedSociedades.map(s => ({sociedad: s}));
I have an multi-dimension array that i'm iterating through. Is there a way to put the contents of the array into a new multi-dimension array creating a new MDA? For example, the following code puts all indices of the original array into the new candies array if there's a match. Currently i'm doing
candies.push([product[0],product[1], etc...]);
I'm just trying to see if there's a faster/cleaner way to get that in there.
I tried:
candies.push(product);
but that didn't work. Here's the code i'm currently using
var sel = 'candy';
var candies = [];
for(var i = 1; i < products.length; i++) {
var product = products[i];
for(var j = 0; j < product.length; j++) {
if(sel==product[11]){
candies.push([product[0],product[1],product[2],product[3],product[4],product[5],product[6],product[7],product[8],product[9],product[10],product[11],product[12]]);
}
break;
}
}
A cleaner way of writing below line:
candies.push([product[0],product[1],product[2],product[3],product[4],product[5],product[6],product[7],product[8],product[9],product[10],product[11],product[12]]);
is:
candies.push(product.slice(0,13)]);
Sample example JSFiddle is here.
Best of luck.
(Mark this as answer if it serves your need.)
I have a single input row that needs to be expanded to create multiple rows, with 6 fields being repeated on each row, and one unique field added to each row. The unique fields are stored in arrparentjobs array, and I know that they have unique values.
When the code runs, the resulting rows all contain the exact same data, which happens to be the values of the last items being pushed.
What am I doing wrong here?
Thanks much,
Joe
var dataRowsOutput = [];
arrVolDataOutput.playerid = volDataRow.playerId;
arrVolDataOutput.timestamp = volDataRow.timestamp;
arrVolDataOutput.playername = volDataRow.playerName;
arrVolDataOutput.parentname = volDataRow.parent1Name;
arrVolDataOutput.parentphone = volDataRow.parent1Phone;
arrVolDataOutput.parentemail = volDataRow.parent1Email;
for (var j = 0; j < arrparentjobs.length; ++j) {
arrVolDataOutput.parentjob = arrparentjobs[j];
dataRowsOutput.push(arrVolDataOutput);
continue;
}
the resulting rows all contain the exact same data
Yes they do and they do because push pushes a reference – not a deep or even shallow copy – onto the array and you're simply changing the parentjob while pushing the exact same arrVolDataOutput object onto the array over and over again.
You need to create a new object on each iteration, something like this:
var dataRowsOutput = [];
for (var j = 0; j < arrparentjobs.length; ++j) {
dataRowsOutput.push({
parentjob: arrparentjobs[j],
playerid: volDataRow.playerId,
timestamp: volDataRow.timestamp,
playername: volDataRow.playerName,
parentname: volDataRow.parent1Name,
parentphone: volDataRow.parent1Phone,
parentemail: volDataRow.parent1Email
});
}
If there are other fields in your arrVolDataOutput then you'll need to include those in the object literal as well.
How to add For loop information to Multi-Dimensional Array?
http://jsfiddle.net/MZj3L/
If I am trying this code get - map undefined. But how to save data something like to this ->
[[Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10]]
Thanks and sorry for my English language.
It seems in you want to initialize a multi dimensional array. Arrays are dynamic in JavaScript, you don't have to initialize them with a certain length. You could just do:
var map = [];
for(var a = 0; a < 10; a++){
map[a] = [];
}
This gives you an array containing 10 arrays.
Why are you getting undefined?
Because your syntax is way of. What map = [a][b]; does is creating an array with one element a and then accessing the bth element of that array and assign it to map.
So in the last iteration, it does:
map = [9][9];
which is the same as
tmp = [9];
map = tmp[9];
try something like
var map = [];
for(var a = 0; a < 10; a++){
map[a]=[];
for(var b = 0; b < 10; b++) {
map[a].push(b);
}
}
I am not sure what you want to do either but that's the only think I could do with your code ...
Hey guys I've got 2 dim array and a hash!
Array's second row values and hash keys are set identical!
What I want is to address each hash key using array's row values and change them to array's current column index
Preview example:
{.....,'_11':val, '_12':value, .....}
arr[1][i]='_12'. use this value to address the the unique hash hey and change that key to i. key=i
Is this the right way?
var keyName;
for(var i=0; i<theLength; i++){
keyName = arr[1][i];
hash.keyName=i;
}
10x for your kind help ,BR
Maybe what you want is this:
var keyName;
for(var i=0; i<theLength; i++) {
keyName = arr[1][i];
hash[keyName] = i;
}
Using hash.keyName will always reference a key called keyName, not the key with that variable name.
Since you don't really need the intermediate variable, you can do this:
for(var i=0; i<theLength; i++) {
hash[arr[1][i]] = i;
}
Not sure I follow what you're asking for the rest, but
hash.keyName=i;
should be:
hash[keyName]=i;