Noob question. Setting array elements throws an error.
I get this error when I run the script: array1[user_id] is undefined.
array1 = new Array();
// the data variable I got from a JSON source
// using jQuery
$.each(data, function(i, item) {
// Set variables
user_id = item.post.user_id;
user = item.post.user;
price = item.post.price;
if (array1[user_id]['user'] != user) {
array1[user_id]['price'] = price;
array1[user_id]['user'] = user;
}
}
First, you should not use an array, if you need a hash map, use objects. In some languages, it's one thing, but in JS they're not.
When defining a nested object you have to define each level as an object.
var hash = {};
// the data variable I got from a JSON source
$.each(data, function(i, item) {
// Set variables
user_id = item.post.user_id;
user = item.post.user;
price = item.post.price;
// Have to define the nested object
if ( !hash[user_id] ) {
hash[user_id] = {};
}
if (hash[user_id]['user'] != user) {
hash[user_id]['price'] = price;
hash[user_id]['user'] = user;
}
}
If I understand this question correctly, you have to initialize the first dimension of array1 first, like so:
array1[user_id] = {};
then you can do this:
array1[user_id]['user'] = 'whatever';
That is of course assuming your user_id is not undefined.
Related
Here is my object:
$scope.info = [];
$scope.info = [{"name":"kanye","size":"","folder":"Folder"},{"name":"west","size":"","folder":"Folder"}]
$scope.infoObj = $scope.info.name;
console.log($scope.infoObj);
This return me Undefined. The response should be like this:
[{"name":kanye},{"name":west}]
But how to access the specific properties from a multiple object using angularJS or jquery/js?
This should solve the problem:
$scope.infoObj = $scope.info.map(function(obj){
var x = {};
x['name'] = obj['name'];
return x;
})
for ES6, it can be simplified to:
$scope.infoObj = $scope.info.map(x => ({name:x['name']}))
You can actually do a little bit of refactoring to make your code a little cleaner and more readable. Instead of setting your info value twice, set it once, and add the objects in each line after.
Like this:
$scope.info = [];
$scope.info.push({
"name":"kanye",
"size":"",
"folder":"Folder"
});
$scope.info.push({
"name":"west",
"size":"",
"folder":"Folder"
});
See how clean that is? Now it should be fairly obvious that info is an Array of objects, so doing $scope.info.name won't work. What I would recommend is creating a lookup function that can help grab a list based on the key you provide it.
Something like this:
function lookup(key) {
var result = [];
for (var i = 0; i < $scope.info.length; i++) {
var object = {};
object[key] = $scope.info[i][key];
result.push(object);
}
return result;
}
And then call it like this:
$scope.infoObj = lookup('name');
console.log($scope.infoObj);
I want to create an object from html elements using JavaScript and jQuery.
The object i want to create is
{
array:[{infraStructureType: 'value', hostId: 'value'}, {infraStructureType: 'value', hostId: 'value'}]
}
So my code to create above object is
var obj = {}, dataObj = {compareESX: []};
$('.checkBox:checked').each(function () {
obj.infraStructureType = $(event.target).attr('hostId');
obj.hostId = $(event.target).attr('infrastructureType');
console.log(obj);
dataObj.compareESX.push(obj);
console.log(dataObj);
});
In above code "console.log(obj)" gives correct output but, when i push it in array "dataObj.compareESX"
Only information of last 'obj' is getting pushed number of times the each loop executes.
JS uses Call by reference method.So when update obj it changes all values. You need to do deep copy.Use this
dataObj.compareESX.push(JSON.parse(JSON.stringify(obj)));
Try this: FIDDLE
We need to define the obj again to clear the previous values.
var dataObj = {compareESX: []};
$('.checkBox:checked').each(function (e) {
var obj = {};
obj.infraStructureType = $(this).attr('hostId');
obj.hostId = $(this).attr('infrastructureType');
//console.log(obj);
dataObj.compareESX.push(obj);
//console.log(dataObj);
});
console.log(dataObj);
You have to put your object definitions var obj = {} inside your each loop. Right now you are using the same object for every entry in the loop. Instead, you should create a new object for every checkbox over which you loop.
var dataObj = {compareESX: []};
$('.checkBox:checked').each(function () {
var obj = {};
obj.infraStructureType = $(event.target).attr('hostId');
obj.hostId = $(event.target).attr('infrastructureType');
console.log(obj);
dataObj.compareESX.push(obj);
console.log(dataObj);
});
I am writing a simple application in node, but having trouble with referencing objects from different modules. The object constructor and methods are (I skipped some methods to keep the excerpt short):
function Account (name, password) {
this._name = name;
this._password = password;
this._attributes = [];
}
Account.prototype.load = function (id) {
var self = this;
self = db.loadObject(id, 'account'); // separate module to save/retrieve data
this._name = self._name;
this._password = self._password;
this._attributes = self._attributes;
return this;
};
Account.prototype.getAttributes = function () {
return this._attributes;
}
Account.prototype.addAttributes = function (a) {
this._attributes.push(a);
};
module.exports = Account;
The db module is nothing fancy at this point:
var fs = require('fs');
var paths = {
'account' : './data/accounts/'
};
function loadObject (name, type) {
var filePath = paths[type] + name + '.json';
if (!fs.existsSync(filePath)) {
return false;
}
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
};
function saveObject (object, type) {
fs.writeFileSync(paths[type] + object.getName() + '.json', JSON.stringify(object),'utf8');
};
exports.loadObject = loadObject;
exports.saveObject = saveObject;
The file is saved as:
{"_name":"John","_password":"1234","_attributes":[["Jane","sub",[[10,1]]]]}
On my caller module I attempt to retrieve the attributes:
var Account = require('./account.js');
var account = new Account();
...
account.load(name);
...
var attr = account.getAttributes();
for (var item in attr) {
console.log(item[0]);
};
...
In the code above, the last loop prints undefined objects. I have checked the files and the information is saved and loaded without any problems. The array attr is not empty. If I print it with:
util.log(typeof attr+': '+attr);
I get:
object: Jane,sub,10,1
Instance issue? Should I rewrite the _attributes to be accessed directly via account.attributes?
This is the code you currently have for outputting your data:
var attr = account.getAttributes();
for (var item in attr) {
console.log(item[0]);
};
What this code does is output to the console the first character of each key in your _attributes field. With the data you've shown in your question, what this outputs is 0 because your _attributes field has this value: [["Jane","sub",[[10,1]]]]. When used in var item in attr the item variable will get only one value the string "0", and item[0] will also evaluate to the string "0". I've actually cut and pasted your code and data into files and ran your code to double check this and this is indeed what I get when I run the code. I don't get undefined values. A more sensible way to get the values out of the array would be:
var attr = account.getAttributes();
for (var item in attr) {
console.log(attr[item]);
}
If you want to iterate two levels deep:
for (var item in attr) {
var value = attr[item];
for (var item2 in value) {
console.log(value[item2]);
}
}
I have the following code:
exports.home = function(Comment,User,Activity){
return function(req, res){
var get_url = req.url.split(/\?/)[1];
if (!req.user)
{
res.writeHead(302, {
'Location': '/'
});
res.end();
return;
}
var posts_id_array = req.user.posts_id_array;
var stocks_array = req.user.watch_list;
var subscribe_to_arr = req.user.subscribe_to;
User.find({_id:{$ne:req.user._id, $nin:subscribe_to_arr}}).sort('-_id').limit(10).exec(function(err_user, users){
Activity.find({$or:[{owner_id : {$in :subscribe_to_arr}},{owner_id:req.user._id}]}).sort('-time_stamp').limit(20).exec(function(err_post,activities){
if( err_post || !activities) {
res.render('home',{user:req.user,stocks:JSON.stringify(stocks_array)});
}
else
{
var funcArr = [];
var hasPost = ["publish","comment","like"];
var notPost = ["add_stock","delete_stock"];
for(var i =0;i<activities.length;i++)
{
if(hasPost.indexOf(activities[i].type)!=-1){
var fobj = {
act: activities[i],
f:function(callback){
var test = this.act;
var comments = test.post.comments;
Comment.find({_id:{$in:comments}},function(err,_comments){
console.log("test.post.comments");
//console.log(test.post.comments);
console.log("comments ");
console.log(_comments);
console.log("type");
console.log(typeof test);
console.log("cloning obj");
// obj = JSON.parse(JSON.stringify(test)); // cloning obj
console.log(test);
console.log("setting value of comments");
**console.log(test.post.comments = _comments);** //unable to change test.post.comments
console.log("after assignment");
console.log(test.post.comments); // remain unchanged but work with obj.post.comments if I clone test as obj and use obj instead.
callback(null,test);
});
}
}
funcArr.push(fobj.f.bind(fobj));
}else{
var fobj = {
act: activities[i],
f :function(callback){
callback(null,this.act);
}
}
funcArr.push(fobj.f.bind(fobj));
}
}
async.series(funcArr,function(err,resArr){
console.log("resArr");
console.log(resArr);
res.render('home',{user:req.user,posts:JSON.stringify(resArr),stocks:JSON.stringify(stocks_array), other_users:JSON.stringify(users)});
});
}
});
}) // end of User.find
}// end of return function(req,res);
}
I want to update the post.comments property of the "test" object (see ** parts), but I was unable to do so. However, when I cloned the "test" object as "obj" then set "obj.post.comments" it works. Why is it the case? Is it because I messed up some scoping issues?
Thanks.
I have solved this problem myself. It turns out that I have store mongodb's Schema.Types.ObjectId in the test.post.comments which after some messing around I found cannot be overwritten. When I create a clone of the test object as "obj", the Schema.Types.ObjectId object in obj.post.comments is stored at a different location which allows for modification. My conjecture is that test.post.comments points to a Schema.Types.ObjectId within mongodb itself and therefore cannot be overwritten. When I create a copy of the test object, the problem is therefore resolved.
var test = this.act.concat();
use this instead.
because arrays substitution in js actually does not copy array but refer original adresses.
for example
var test = ['A','B','C','D'];
var copied = test;
test[0] = 0;
copied[1] = 0;
console.log(test) //0,0,'C','D'
console.log(copied) //0,0,'C','D'
so to avoid this issue, You can use .concat() to copy array
if you do not add anything, it will be used as copying.
var test = ['A','B','C','D'];
var copied = test.concat();
test[0] = 0;
copied[1] = 0;
console.log(test) //0,'B','C','D'
console.log(copied) //'A',0,'C','D'
I have a variable with a value, let's say
var myVarMAX = 5;
In HTML I have an element with id="myVar".
I combine the id with the string MAX (creating a string myVarMAX). My question is how can I use this string to access a variable with the same name?
You COULD use eval, but if you have the var in the window scope, this is better
var myVarMAX = 5;
var id="MAX"; // likely not in a var
alert(window["myVar"+id]); // alerts 5
However Don't pollute the global scope!
A better solution is something like what is suggested in the link I posted
var myVars = {
"myVarMin":1,
"myVarMax":5,
"otherVarXX":"fred"
} // notice no comma after the last var
then you have
alert(myVars["myVar"+id]);
Since this post is referred to often, I would like to add a use case.
It is probably often a PHP programmer who gives Javascript/Nodejs a try, who runs into this problem.
// my variables in PHP
$dogs = [...]; //dog values
$cats = [...]; //cat values
$sheep = [...]; //sheep values
Let's say I want to save them each in their own file (dogs.json, cats.json, sheep.json), not all at the same time, without creating functions like savedogs, savecats, savesheep. An example command would be save('dogs')
In PHP it works like this:
function save($animal) {
if(!$animal) return false;
file_put_contents($animal.'.json', json_encode($$animal));
return true;
}
In Nodejs/Javascript it could be done like this
// my variables in NodeJS/Javascript
let dogs = [...]; //dog values
let cats = [...]; //cat values
let sheep = [...]; //sheep values
function save(animal) {
if (!animal) return false;
let animalType = {};
animalType.dogs = dogs;
animalType.cats = cats;
animalType.sheep = sheep;
fs.writeFile(animal + '.json', JSON.stringify(animalType[animal]), function (err){
if (err) return false;
});
return true;
}