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?
Related
How can ı equalize my array ıd and my value ıd and access value.name I didn't do it
This is my code:
activity(val) {
var act = this.items.map(function (val) {
if (element.ActivityID== val) {
return element.ActivityName
}
return act
});
Perhaps this?
activity (val) {
const activity = this.items.find(item => item.ActivityID === val)
return activity && activity.ActivityName
}
This just finds the item with the corresponding ActivityID and then returns its ActivityName.
Your original code contained several possible mistakes:
Two different things called val.
element doesn't appear to be defined.
The return act was inside the map callback. The activity method itself wasn't returning anything.
Not really clear why you were using map to find a single item. map is used to create a new array with the same length as the original array with each item in the new array determined by the equivalent item in the original array. It 'maps' the items of the input array to the items in the output array.
I am pushing an object into an array but cannot do it?
I'm doing it like this
this.passData = this.tribeForm.value;
var id = {"tribe_id": 1}
this.passData.push(id)
This is the value in the tribeForm
I also tried
var id = {tribe_id: 1}
and
this.passData.splice(0,0, id)
and
this.passData = Array.prototype.slice(id)
and
this.passData.concat(id)
but it all ends up with
TypeError: this.passData.push/splice/concat is not a function
The question is not that clear, But I understood you are manipulating form data, value of form data returns an Object, Not an array. Objects in JavaScript are represented as key-value pairs, (or attribute-value) pairs.
Example :
var object = {
name : "Jhon",
grade : 12,
gpa : 8.12
}
It is just a collection of key-value pairs, push(), concat() and other methods are supported only for Arrays not for Objects. You can achieve whatever you want simply by creating a new key/attribute and assigning the value to it.
this.passData = this.tribeForm.value
this.passData['tribe_id'] = 1
//or, Objects can also contain nested object
this.passData['someKey'] = {'tribe_id' : 1}
You can create an empty array and push objects to it
Example :
var exampleArray = []
exampleArray.push({'tribe_id' : 1})
Now, it works because exampleArray is an Array not JS object.
Thanks for A2A
First, you need to understand the error:
TypeError: this.passData.push/splice/concat is not a function
Push/splice/concat is functions for Array and because of that the console is yelling at you that the passData is not an Array.
Make sure your passData is an Array and you will able to do so.
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);
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.
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);
});
});