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}));
Related
so I'm looping through a json response and I'm trying to use the counter (var i) to say data.newarray[i].time+i so with each loop the next array is chosen and the time also increases in number. So 1st loop will spit out data.newarray[0].time0 then data.newarray[1].time1 then data.newarray[2].time2 and so on. The bit that is currently failing is my concatenation time+i at the end. How do I format this to work?
var data = JSON.parse(xmlHTTP.responseText);
for(var i=0; i<data.newarray.length; i++)
{
alert(data.newarray[i].time+i);
}
You can access variable property names by using the quoted notation: obj['prop'] instead of obj.prop.
The solution is:
var data = JSON.parse(xmlHTTP.responseText);
for(var i=0; i<data.newarray.length; i++)
{
alert(data.newarray[i]['time'+i]);
}
Try something like this:
for(var i=0; i<data.newarray.length; i++) {
alert(data.newarray[i]['time'+i]);
}
I'm pulling data with Ajax that changes every day. I want to package the data into objects that have unique names. Currently I created a constructor that looks like something like this:
function myObject(Id,thing1,thing2,thing3)
{
this.Id = Id;
this.thing1 = thing1;
this.thing2 = thing2;
this.thing3 = thing3;
}
then I push that to an array in a loop like this
for(var i=0; i<data.length; i++)
{
array.push(new myObject(value1,value2,value3,value4));
}
This works fine for what I'm doing but I just get an array with [object,object,object,object] inside of it which I can access with array[0] , array[1], etc.
but now I want to store those objects in firebase and would need to reference them so how could I have them named uniquely?
normally you would do
var thingid1 = new myObject(value1,value2,value3,value4));
var thingid2 = new myObject(value1,value2,value3,value4));
but this is all being created on the fly and sometimes there is 1 object created and sometimes 10.
I'm new at this and I've looked everywhere so any help would be appreciated.
If your Id (value1) is unique...
var myObjectContainer = {};
for(var i=0; i<data.length; i += 1)
{
myObjectContainer[value1] = new MyObject(value1,value2,value3,value4);
}
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;