I'm generating objects from an array which I've defined like this (It's not limited to these three):
var links = [['Linkedin','img/linkedin.png','-300','-230', '600'],
['Google+', 'img/google.png', '0', '-230', '600'],
['Twitter', 'img/twitter.png', '300', '-230', '600']];
Now it goes through the each loop to create and add the objects to the scene by Three.JS like this:
$.each(links, function(i, item) {
var thisItemTexture = THREE.ImageUtils.loadTexture(item[1]);
thisItemGeo = new THREE.CubeGeometry(60, 60, 60,1 ,1 , 1);
thisItemMat = new THREE.MeshBasicMaterial({map: thisItemTexture });
thisItem = new THREE.Mesh(thisItemGeo, thisItemMat);
scene.add(thisItem);
thisItem.position.x = item[2];
thisItem.position.y = item[3];
thisItem.position.z = item[4];
thisItem.castShadow = true;
thisItem.receiveShadow = true;
});
The question is: How can I access the objects that I've made in the each loop above?
You can do this:
myObject.name = "objectName";
...
var object = scene.getObjectByName( "objectName" );
or to recursively search the scene graph
var object = scene.getObjectByName( "objectName", true );
Alternatively, you can search by ID.
var object = scene.getObjectById( 4, true );
three.js r.61
Related
What I am trying to do is be easily explain from the code.
How can I achieve in getting the correct result without creating a new object every time?
var optionsArr = new Array();
var labelObj = new Object();
labelObj.label = 'Pink';
labelObj.value = "Pink1";
optionsArr.push(labelObj);
labelObj.label = 'Green';
labelObj.value = "Green2";
optionsArr.push(labelObj);
console.log('optionsArr' + JSON.stringify(optionsArr));
expected result : {"label":"Pink","value":"Pink1"},{"label":"Green","value":"Green2"}
actual result : {"label":"Green","value":"Green2"},{"label":"Green","value":"Green2"}
var obj = [];
obj[0] = {"label" : "Pink","value" : "Pink1"};
obj[1] = {"label" : "Green","value" : "Green1"};
alert(JSON.stringify(obj));
OR the more proper way to handle an array:
var obj = [];
obj.push({
label: 'Green',
value: 'Green2'
});
obj.push({
label: 'Pink',
value: 'Pink1'
});
alert(JSON.stringify(obj));
In JavaScript objects are reference-type values, so there is no way to pass object value except for creating a new object, so you're limited to Object.assign option. The approach below would change desired properties and copy other ones:
var optionsArr = new Array();
var labelObj = new Object();
labelObj.label = 'Pink';
labelObj.value = "Pink1";
optionsArr.push(labelObj);
// Use Object.assign() to update only some of properties
optionsArr.push(Object.assign({}, labelObj, {
label: 'Green',
value: 'Green2'
}));
// Original object would remain unchaged
optionsArr.push(labelObj);
console.log('optionsArr' + JSON.stringify(optionsArr));
Is there some kind of way of having some kind of "shared" variable? What I want to do is this:
var id = 5;
var player = new Player(id);
var array1[0] = player;
var array2[0] = player;
array1[0].id = 8
console.log(array1[0]); //8
console.log(array2[0]); //8
In JavaScript, you do not store an object directly in a variable, but rather a reference to the object.
That means, you can have two variables that point to the same object - you simply copy the reference:
var a = {test: "test"};
var b = a;
console.log(a === b) // true
In this case, if you mutate the object via a and later read it via b, you will see the changes.
With the right implementation of Player, you can make that work for you:
var Player = function(id) {
this.id = id;
}
Player.prototype.setId = function(id) {
this.id = id;
}
Player.prototype.getId = function() {
return this.id;
}
var player = new Player(5);
console.log(player.getId()); // 5
var arrA = [];
var arrB = [];
arrA.push(player);
arrB.push(player);
console.log(arrA[0].getId()); // 5
console.log(arrB[0].getId()); // 5
arrA[0].setId(10);
console.log(arrA[0].getId()); // 10
console.log(arrB[0].getId()); // 10
Check MDN for more info on working with objects.
I wanted to know if its possible to ad elements to an array which is declared as the following...
Please check the add() function, I can't figure out how to solve this problem. Thanks
It's not necessary, but I'd appreciate if you give an explanation since of c++ point of view programmer.
// My array is this way declared
var myArray = [
['John', 'Doe', '1980'],
['Jane','Malloy','1982'],
['Vincent','Malloy','1972']
];
// then I want to add a new elements in it, but It seems to doesn't work
var add = function() {
//var textbox = document.getElementById('textbox').value;
// storing new person in array
myArray [3][0] = 'New1';
myArray [3][1] = 'New2';
myArray [3][2] = 'New3';
};
//finally this function is for displaying the elements of myArray
var show = function() {
// clean output
document.getElementById('output').innerHTML = '';
// delay time
setTimeout (function() {
// showing info. people
for (var i in myArray) {
for (var j in myArray)
document.getElementById('output').innerHTML += myArray[i][j] + ' ';
document.getElementById('output').innerHTML += '<br/>';
}
}, 250);
};
So right here:
var add = function() {
//var textbox = document.getElementById('textbox').value;
// storing new person in array
myArray [3][0] = 'New1';
myArray [3][1] = 'New2';
myArray [3][2] = 'New3';
};
You can't add to myArray[3] because myArray[3] is undefined. You need to assign an empty array to myArray[3] first:
myArray [3] = [];
myArray [3][0] = 'New1';
myArray [3][1] = 'New2';
myArray [3][2] = 'New3';
Or more generally, assuming the idea is to add to the end of your array, you could do something like:
var idx = myArray.length;
myArray[idx] = [];
myArray[idx][0] = "New 1";
// ...
Or even something like:
var newArray = ["New1", "New2", "New3"];
myArray.push(newArray);
I have this function:
flotLinea:function(tareas){
var self = this;
console.info(tareas);
var aTar = new Array();
for(var i = 0;i<tareas.length;i++){
var val = new Array(new Date(tareas[i].fecha_registro),tareas[i].porcentaje);
aTar.push(val);
}
console.info(aTar);
},
Using console.info(tareas); print this :
And using console.info(aTar); print :
(The data from tareas always is changing because the data comes from a dropdown)
I need create an new array for each id_usu using the same data , how can I do this?
For example in this case I need an array for id_usu = 4 ( are two id_usu = 4, so i need the data where id_usu = 4) , one array with id_usu = 6 and one array with id_usu = 9
I need do this , because this data are for a chart, so, after , each user ( id_usu ) will be a different color in that chart.
From whatever I have understood form your problem statement and the code you have provided, I've provided a solution below.
flotLinea:function(tareas){
var self = this;
console.info(tareas);
var aTar = new Array();
var idArray = [];
for(var i = 0;i<tareas.length;i++){
if(idArray.indexOf(tareas[i].id_usu) == -1){
var val = new Array(new Date(tareas[i].fecha_registro),
tareas[i].porcentaje);
idArray.push(tareas[i].id_usu);
aTar.push(val);
}
else{
for(var j = 0; j < aTar.length; j++){
if(tareas[i].id_usu == aTar[j][0].id_usu){
aTar[j].length = new Array(new Date(tareas[i].fecha_registro)
,tareas[i].porcentaje);
}
}
}
}
console.info(aTar);
}
I'm using Brute-Force kind of solution, performance can always be increased.
I've created on new array above as idArray to hold the unique id_usus, and am comparing if the current tareas[i].id_usu already is there in that array, if not push the new value to aTar array and tareas[i].id_usu to idArray, else loop over the aTar array and find the array which already has the object with the current tareas[i].id_usu and push the new values at aTar[j].length.
I am working on a jquery form file that uses an array to populate a dropdown of sizes based on the selection of the material. This works fine but now we are adding an additional dropdown and we need to have different values in the array based on that selection.
this is the part of the current working code:
var Material = new Array();
Material['UNC'] = new Array('40', '32');
Material['UNF'] = new Array('10', '24');
This is basically what I am trying to, not sure how:
if($("#style").val() == "long") { var Material = new Array();
Material['UNC'] = new Array('45', '35');
Material['UNF'] = new Array('15', '29');} else {
var Material = new Array();
Material['UNC'] = new Array('40', '32');
Material['UNF'] = new Array('10', '24');}
I'm not having any luck, I'm not super familiar with Javascript functions. Thanks
One way:
var isLong = $('#style').val() === 'long';
var material = {};
material.UNC = (isLong) ? [45, 35] : [40, 32];
material.UNF = (isLong) ? [15, 29] : [10, 24];
Another way:
var isLong = $('#style').val() === 'long';
var material = {};
if (isLong) {
material.UNC = [45, 35];
material.UNF = [15, 29];
}
else {
material.UNC = [40, 32];
material.UNF = [10, 24];
}
As Felix Kling points out, it is better to use an object over an array for material. I've also used JavaScript convention of a lowercase variable name. Instead of using new Array use [] and instead of new Object, you can use {}.
You just need to move the declaration of the Material variable outside the blocks:
var Material = new Array();
if($("#style").val() == "long") {
Material['UNC'] = new Array('45', '35');
Material['UNF'] = new Array('15', '29');
} else {
Material['UNC'] = new Array('40', '32');
Material['UNF'] = new Array('10', '24');
}
However, as others have pointed out, you should be using an object rather than an array for these kinds of non-numeric indexes. You should also use object and array notation:
var Material = {};
if($("#style").val() == "long") {
Material['UNC'] = ['45', '35'];
Material['UNF'] = ['15', '29'];
} else {
Material['UNC'] = ['40', '32'];
Material['UNF'] = ['10', '24'];
}