AngularJS foreach - producing numerous null for unassigned values - angularjs

I have a array variable viewedprofiles= []; initialized.
I'll be assigning the profiles that have been viewed to this viewedprofiles array. Now if I try to display it (By assigning it to a $scope), I get NULL for other values that have not got assigned or touched.
var viewedprofiles= [];
angular.forEach(profiles, function(value, key){
if(value.viewed== "yes") {
viewedprofiles[value.id] = TRUE;
}
});
The output of viewedprofiles is as follows
[NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,TRUE]
Output explanation :
Since the 9th id's profile viewed value was yes, the output returned TRUE at the 9th element of the viewedprofiles array.
Nothing wrong actually.
But I was wondering as far as the above code, the id was TRUE for 9th element. What if the id was some large number say 15640, Will there be 15639 NULLs before TRUE? Am I doing anything wrong or is there another way to work this out?

I found the answer.
What I was trying to do was basically wrong at the assigning part. I should push the element instead of assigning.
The following worked.
angular.forEach(profiles, function(value, key){
if(value.viewed== "yes") {
viewedprofiles.push(value.id);
}
}, viewedprofiles);

Related

Google Script is returning the index of the array but I need the value

I have a google spreadsheet that gets data logged to it via a google form.
When the form is logged each time, it triggers a script that gets values from a certain section using:
var tabNumsVal = sheet.getSheetValues(lastRow, tabOneCol.getColumn(), 1, 6)[0];
When I check the array, I can see that the array has the values such as:
0: 12
1: 24
2: 26W
3: 0
4: 0
5: 0
However when I use the following command, it puts the index numbers (0 to 5) into the array instead of the values in the array.
var tabNumsFinal = [];
for (var tabard in tabNumsVal) {
if (tabard !== "") {
tabNumsFinal.push(tabard);
}
}
It used to work but I have had to upgrade my code to Google Script v8 and it seems that this has broken the code.
I had to alter the 'for each' code block to a 'for' code block and it seems this is handling the values differently.
I am quite sure this is simple for many people but I really only touch Google Script 1 time each year. I have tried using Logger.log(tabard) to output the data to the execution log, but it just traverses the code and doesn't output anything. I figured this might be because of the !== "" operator, so I placed it above the if statement but still inside the for statement and it still outputs nothing.
I tried using Logger.log(tabNumsVal) and Logger.log(tabNumsFinal) and again it output nothing.
To recap:
The data from the form is returning correctly into the columns of the spreadsheet, hence it is showing inside the array properly. It's just that the index numbers are being output instead of the values from the array.
Since you're using for in loop, tabard is the index here.
var tabNumsFinal = [];
for (var i in tabNumsVal) {
let val = tabNumsVal[i];
if (val !== "") {
tabNumsFinal.push(val);
}
}
For in loop

How to fetch array value having String Key using Angular

I have an array and want to fetch some values from array which has Strings as key.Please suggest how can i retrieve those values from array have string as key.
Code for Controller is:
var ultColumn=undefined;
$scope.ultColm="Attained Age";
for(var i=0;i<5;i++){
ultColumn=ultrowCellData[i][$scope.ultColmn];//This is not working
}//ultrowCellData contains the array
Please suggest how to get the value of key "Attained Age"
You can use angular.forEach(); For example:
angular.forEach(yourArray, function(value, key){
if(typeof key === 'string'){
console.log("Your result is here :", value);
}
});
Thanks.
If screenshot which you provided shows data included in ultrowCellData then it is not an array but map - var something = ultrowCellData['Attained Age '] will assign value 28 to something
You seem to have forgotten a space at the end of your key, on line 2.
Try Attained Age<space> instead of Attained Age (replace <space> with an actual space).
Notice, though, that this is a non-standard use for an Array, as arrays usually use only numbers as keys.
if at all possible, try using an Object instead.

Sequelize doesn't return anything if value for $notIn is an empty array but it returns me if array contains an empty value

I want Sequelize to return to me all the values which IDs are not in an array.
Sequelize doesn't return anything if value for $notIn is an empty array but it returns me if array contains an empty value.
This returns me nothing:
db.foo.findAll({
where: {
id: {
$notIn: []
}
}
});
This returns me every value:
db.foo.findAll({
where: {
id: {
$notIn: ['']
}
}
});
How come it doesn't return all values if the array is empty? If it is empty, then it means all values which values are not in that array should be returned. Since the ID doesn't contain any value, sequelize should return to me all the values, right?
This issue will be fixed in Sequelize version 4. That version has not been fully disclosed yet, though. At least there is no documentation yet.
See the GitHub issue at https://github.com/sequelize/sequelize/issues/4859.
The changelog can be found at https://github.com/sequelize/sequelize/blob/master/changelog.md. It says
[FIXED] $notIn: [] is now converted to NOT IN (NULL) #4859
Maybe you can implement those version 4 changes in your current code yourself.
For those using < 4 version, here is a work around I used,
let idsToSkip = [...]; //generate array of ids, this may result in empty array
idsToSkip.push('-1'); // push a value that can not be the id in table. this will generate `not in ('-1')` query serving the purpose.
db.foo.findAll({
where: {
id: {
$notIn: idsToSkip
}
}
});
Hope it helps !!

Angular concat array

In my Angular controller, i loop through an array of cities to retrieve each value sent :
angular.forEach($scope.outPutcities, function (value, key) {
// value.id are 3,4
$scope.countries.cityId = value.id;
$scope.cities.push($scope.countries.cityId)
});
But the cityId value has always the last value in the array which is 4.
[Object { countryName=Canada, cityId=**4**}, Object {countryName=Canada, cityId=**4**]
But what i want is :
[Object { countryName=Canada, cityId=**3**}, Object {countryName=Canada, cityId=**4**]
Is there an easy to fix this ? Thanks
Firstly this is redundant
$scope.countries.cityId = value.id;
You are assigning to countries.cityId and overwriting it on each iteration, rather just do
$scope.cities.push(value.id)
Your code does look fine besides that, are you sure the $scope.outPutcities has the values you are expecting?

Getting Keys and Values from JSON

For me i have a object like var object = [{"1":"w"},{"2":"z"}] ;
While iterating other array = '[{},{},{}]' i wanted to get object key and value i.e processing at index 0 of array should give me 1 and w respectively .
. While seeing some other posts of stack overflow i tried with ,
$.each(array, function(index, value) {
if(object[index] != undefined)
{
console.log("enterobject",$.parseJSON(JSON.stringify(object[index])));
console.log("enterobjectValue",$.parseJSON(JSON.stringify(object[index])).key);
console.log("enterobjectValue",$.parseJSON(JSON.stringify(object[index])).value);
}}
Only first console.log is printing like {"1":"w"} for index 0 , but not second and third log which i wanted to return me 1 and w respectively are not working .
Thanks
It looks like you're getting back an array. If it's always going to consist of just one element, you could do this (yes, it's pretty much the same thing as Tomalak's answer):
$.each(result[0], function(key, value){
console.log(key, value);
});
If you might have more than one element and you'd like to iterate over them all, you could nest $.each():
$.each(result, function(key, value){
$.each(value, function(key, value){
console.log(key, value);
});
});

Resources