im taking first steps in angular, and i need to return array excluding empty spaces in it. i was able to create function to return the array i want, spliced, but couldn't find a way to make sure each cell in the array contains a value.
this is my function:
$scope.letters = function(arr) {
var lettersarray = arr.split('');
return lettersarray;
};
is there a way to do it in angular?
i know a way in jquery but understood its not recommended to mix both so im looking for an "angularish" way to do it..thx
There's no real "Angular" way to do it, why not just use native JS and .map
function trimArraySpaces(arr) {
return arr.map(function(item) {
return item.trim();
});
}
var testArr = ["John ", "Sally "],
trimmedArr = trimArraySpaces(testArr); //["John", "Sally"]
Related
I have a function in swift and I am using a for loop in it.
let pointTF = [emailTF,passTF,resetEmailTF]
for i in 0...pointTF.count
{
return coachMarksController.helper.makeCoachMark(for: pointTF[i])
}
I want to return this value outside the loop? How can I do that? Please help
Assuming the above code is wrapped in a function you can define an array of CoachMark objects outside the scope of the for loop, and append the generated coachMark to the array:
var coachMarks: [CoachMark]()
for i in 0...pointTF.count
{
var cm = coachMarksController.helper.makeCoachMark(for: pointTF[i])
coachMarks.append(cm)
}
//Then you can return a single CoachMark:
return coachMarks[1]
Don't really know what you are returning, but if you want to return an array of the results you loop through, i suggest using a map for it, neat and simple.
return pointTF.map({ coachMarksController.helper.makeCoachMark(for: $0) })
I have a computed array which is full of tags and updates depending on what selection i make in the select box. I would like to take this array and pass it to a method and then run a method to update what “results” have an active class. Although I get an array saying I can’t run forEach on this element.
Been through a few topics and understand computed properties dont work like that but surely there is a way around this.
https://jsfiddle.net/39jb3fzw/6/
Short Snippet
methods: {
updateOutput() {
var tags = this.tagArray;
tags.forEach(function(tag) {
console.log(tag);
})
}
},
computed: {
concatenated: function () {
var ret = this.selected.concat(this.selected2, this.selected3);
this.tagArray = ret;
//this.updateOutput();
return ret;
}
}
Full Output
https://jsfiddle.net/39jb3fzw/6/
Thanks again :slight_smile:
It looks like the issue is the line:
var ret = this.selected.concat(this.selected2, this.selected3);
That line of code is returning an empty string rather than an array. This is because this.selectedX is a string rather than an Array. This explains why tag.forEach is undefined. forEach doesn't exist on the String prototype.
You can create this an array instead be doing
var ret = [ this.selected, this.selected2, this.selected3 ]
From there you can set this.tagArray to ret
Hope this helps
I am using Firebase and the AngularFire library. I am looking for a way to remove all items or a range of items from a $firebaseArray object. I don't see a straightforward way to do it in the documentation. Is there some way I'm not thinking of other than looping and removing items one by one? Please tell me that's not the only way!!
If there isn't a method in the $firebaseArray that does what you want, you can use the array's $ref() to perform Firebase SDK-style calls. The array content will be synchronized with the changes you make through the ref.
To delete all elements, call remove on the ref itself:
function removeAll(firebaseArray) {
return firebaseArray.$ref().remove();
}
To remove a range, perform an update in which the keys to be removed are set to null:
function removeRange(firebaseArray, start, end) {
var keys = {};
if (end === undefined) {
end = firebaseArray.length;
}
for (var i = start; i < end; ++i) {
keys[firebaseArray.$keyAt(i)] = null;
}
return firebaseArray.$ref().update(keys);
}
Both functions return promises.
I couldn't get the firebaseArray.$ref().remove() to work as the remove() function didn't exist on the object when ordered by child, but doing the following seemed to work though:
$firebaseUtils.doRemove(firebaseArray.$ref());
Following Situation:
role: { roleid=3, name="admin"}
availableRoles:
[
{ roleid=3, name="admin", $$hashKey="object:222"},
{ roleid=4, name="plain user", $$hashKey="object:223"}
]
currentRoles:
[
{ roleid=3, name="admin"}
]
Following Trys:
currentRoles.indexOf(role); // works properly and outputs 0
availableRoles.indexOf(role); // does not work
I can imagine, this occurs because of $$hasKeys. But I didn't put them there, AngularJS does augment these data.
How can I overcome this situation?
Is there a function like: ignore Angular HasKeys in this Datastructure?
Edit:
Angular object comparison:
Compare objects in Angular
So you can just write the function:
function arrayObjectIndexOf(arr, obj){
for(var i = 0; i < arr.length; i++){
if(angular.equals(arr[i], obj)){
return i;
}
};
return -1;
}
--ORIGINAL--
JavaScript saves objects as pointers, therefore, two objects even if has the same data in them, have different values (the value of the pointer in the memory).
Code example:
var role = { roleid:3, name:"admin"};
var availableRoles =
[
{ roleid:3, name:"admin"},
{ roleid:4, name:"plain user", $$hashKey:"object:223"}
];
alert(availableRoles.indexOf(role));
http://codepen.io/anon/pen/BjobaW
So it does not relate to the hashKey. To compare to objects (and such, find the index in an array) you must create a loop of comparison, or overload the "==" operator of Object to compare values and not pointers, which I dont believe you are allowed to do in JS.
Best way is not to have such objects...
You can use angular filter:
function contains(arr, id) {
return $filter('filter')(arr, {roleid : id}, true).length != 0;
}
You can use some other js library (lodash, underscore, ...) for such things.
I have the current JSON file:
[{"id":"1","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]},
[{"id":"2","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]}
How do I select the array with ID 1 and list every 'img_id' inside it, without repeating it for the other array?
edit 1:
I am trying to parse it like this but this code is erroneous:
$("#button").click(function() {
$.getJSON("../path/to/json", function(data) {
$.each(data[0].images.img_id, function(i,data){
var new_data ="<p src='path/to/folder/"+images.img_id+"'></p>";
$(new_data).appendTo("#htmlTag");
});
}); return false;
});
Much appreciated.
You have an array of objects, where each object contains another array of objects. I'm assuming the JSON structure you are using is:
var a = [{"id":"1","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]},
{"id":"2","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]}];
I'm also assuming you are using JavaScript.
PostEdit:
Your code is fairly close, I believe what I have below should work:
$("#button").click(function() {
$.getJSON("../path/to/json", function(data) {
$.each(data[0].images, function(i,data){
var new_data ="<p src='path/to/folder/" + data.img_id + "'></p>";
$(new_data).appendTo("#htmlTag");
});
});
return false;
});
All I did was change the first parameter to your each call from: data[0].images.img_id to: data[0].images. Also, I changed the declaration of new_data from:
var new_data ="<p src='path/to/folder/"+images.img_id+"'></p>";
to:
var new_data ="<p src='path/to/folder/"+data.img_id+"'></p>";
Note that the parameter "data" in the each callback function is simply the element in the array, while "i" is the index of that element in the array. Therefore, data is an object which looks like this:
{"img_id":1}
So, you can get the ID via data.img_id. Hope this helps.
I think you're talking about references. I don't think they are possible in JSON. In case you strictly need them and still want the readable serialization of your objects - I'd suggest you to look into YAML
I think there is a typo in your JSON...it seems like a bracket is missing at the end, and one has been added at the start of line 2. But, assuming you meant this (and are using Javascript):
var myJson = [{"id":"1","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]},
{"id":"2","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]}];
then you can access whatever you need just like a nested Javascript object. If you wanted to only access the object with ID equal to 1, and order is not guaranteed, you would have to iterate:
for(var i = 0; i < myJson.length; i++){
if(myJson[i].id === "1"){
var imgs = myJson[i].images;
for(var j = 0; j < imgs.length; j++){
//do what you want with imgs[j].img_id
}
}
}