This is my controller code:
var domainList=angular.copy($scope.busdomain);
if(domainList!=null){
domainList[0].childNode.sort();
//For finding out the length of list
for (var i = 0; i < domainList[0].childNode.length; i++) {
//Here I want to add a check that I can get name only of objects in my list
//Please see the attached Object screenshot for better understanding
if (domainList[0].childNode[i].name!=null) {
var name=domainList[0].childNode[i].name;
domainList[0].childNode.splice(i,1,name);
$scope.busdomainname=domainList[0].childNode;
}
}
I want to get the list of object and take out its name and add to $scope.busdomainname as I need to display only name on another page.
Is there any way to do this. Please help me out. Thanks in advance
Try underscore library example instead of going the hard way. It provides useful functions like this. Your case
_.pluck (domainList, "name");
Related
Can anyone please help me. I am appending the data in local storage and it is taking duplicate values as well. Anyone know how to ignore duplicate values and append only unique values? I tried to check other similar types of stack-overflow, but not work for me, please help me!
Please see the attached code and its output for reference:-
getProgramDetails = (ppTermDetails) => {
this.programDetails = ppTermDetails;
var entry = this.programDetails;
sessionStorage.setItem("entry", JSON.stringify(entry));
var allEntries = JSON.parse(sessionStorage.getItem("allEntries")) || [];
allEntries.push(entry);
sessionStorage.setItem("allEntries", JSON.stringify(allEntries));
};
Please see the attached output for better understanding
Maybe a better way would be to use object and use programPerTermId as a key and so the new one would override the old one rather than having to loop through the array to find the duplicate one
assuming the programPerTermId is unique
{
75: {programPerTermId: 75,....}
}
getProgramDetails = (ppTermDetails) => {
sessionStorage.setItem("entry", JSON.stringify(ppTermDetails));
var allEntries = JSON.parse(sessionStorage.getItem("allEntries")) || {};
//we will use the programPerTermId as a key for the object
//and will store the object as the value
allEntries[ppTermDetails.programPerTermId] = ppTermDetails;
sessionStorage.setItem("allEntries", JSON.stringify(allEntries));
};
but if you still want to go with your idea then you can check this thread and this, check the comments on the same topic
I am trying to write an html page for class that uses a drop down menu to allow users to pull up a list of relevant information. Unfortunately I am having trouble figuring out how to make the script call on the information in the array. The jsfiddle has the full html section, any help would be GREATLY appreciated.
Please bear in mind that I am not very good with terminology, so be as specific as possible. Especially regarding jQuery, our teacher didn't go over it much so it's a freaking mystery to me.
Also, I do plan on adding more information to the objects in the array, but until I get it working, I don't want to waste the time on something I might need to restructure.
http://jsfiddle.net/GamerGorman20/nw8Ln6ha/11/
var favWebComics = [
Goblins = {1: "www.goblinscomic.org"},
GirlGenious = {1: "www.girlgeniousonline.com"},
GrrlPower = {1: "www.grrlpowercomic.com"}
];
var myFunction = function() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
document.getElementById("web").innerHTML = favWebComics.x;
};
Again, the JSFiddle link has the full html, there are some unused items currently, but I do plan on adding more of them soon.
My next plan is to incorporate images into the objects, so a picture loads for each selection option. How would I manage that?
[ ] is used for arrays, which are indexed with numbers. If you want named properties, you should use an object, which uses { } for its literals:
var favWebComics = {
Goblins: "www.goblinscomic.org",
GirlGenious: "www.girlgeniousonline.com",
GrrlPower: "www.grrlpowercomic.com"
};
= is for assigning to variables, not specifying property names in an object.
Then you need to understand the difference between . and [] notation for accessing objects. .x means to look for a property literally named x, [x] means to use the value of x as the property name. See Dynamically access object property using variable.
So it should be:
document.getElementById("web").innerHTML = favWebComics[x];
your array is not structured correctly and an object would be better suited:
var favWebComics = {
Goblins : "www.goblinscomic.org",
GirlGenious : "www.girlgeniousonline.com",
GrrlPower : "www.grrlpowercomic.com"
};
then you should be able to access the properties as you intend
favWebComics.Goblins
favWebComics.GirlGenious
favWebComics.GrrlPower
Technically you were treating the array like a dictionary. if you're going to do that but still wanna add more information later you'll need to use brackets {} on the code.
var favWebComics = {
Goblins: ["www.goblinscomic.org"],
GirlGenious: ["www.girlgeniousonline.com"],
GrrlPower: ["www.grrlpowercomic.com"]
};
Also for javascript, as long as your searching key value stores, use braces [] for the call. Here's the working code below.
document.getElementById("web").innerHTML = favWebComics[x];
I have your solution, that displays:
the selected choice
the url
the images
Please check the fiddle.
http://jsfiddle.net/nw8Ln6ha/13/
Your object would be:
var favWebComics = {
Goblins : {url:"www.goblinscomic.org", img:"img1"},
GirlGenious : {url:"www.girlgeniousonline.com", img:"img2"},
GrrlPower : {url:"www.grrlpowercomic.com", img:"img3"}
};
Your display code:
document.getElementById("demo").innerHTML = "You selected: "+x+" "+ eval("favWebComics[\""+x+"\"].url")+" "+ eval("favWebComics[\""+x+"\"].img");
First question ever, new to programming. I'll try to be as concise as possible.
What I want to do is to create a bunch of children inside a selected div and give each of them specific html content (from a predefined array) and a different id to each child.
I created this loop for the effect:
Game.showOptions = function() {
var i = 0;
Game.choiceElement.html("");
for (i=0; i<Game.event[Game.state].options.length; i++) {
Game.choiceElement.append(Game.event[Game.state].options[i].response);
Game.choiceElement.children()[i].attr("id","choice1");
}
};
Using the predefined values of an array:
Game.event[0] = { text: "Hello, welcome.",
options: [{response: "<a><p>1. Um, hello...</p></a>"},
{response: "<a><p>2. How are you?</p></a>"}]
};
This method does not seem to be working, because the loop stops running after only one iteration. I sincerely have no idea why. If there is a completely different way of getting what I need, I'm all ears.
If I define the id attribute of each individual p inside the array, it works, but I want to avoid that.
The idea is creating a fully functional algorithm for dialogue choices (text-based rpg style) that would work with a predefined array.
Thanks in advance.
The problem with your loop as I see it could be in a couple different places. Here are three things you should check for, and that I am assuming you have but just didn't show us...
Is Game defined as an object?
var Game = {};
Is event defined as an array?
Game.event = new Array();
Is Game.state returning a number, and the appropriate number at that? I imagine this would be a little more dynamic then I have written here, but hopefully you'll get the idea.
Game.state = 0;
Now assuming all of the above is working properly...
Use eq(i) instead of [i].
for (var i = 0; i<Game.event[Game.state].options.length; i++) {
Game.choiceElement.append(Game.event[Game.state].options[i].response);
Game.choiceElement.children().eq(i).attr("id","choice" + (i + 1));
}
Here is the JSFiddle.
Attempting to build a resume creator as a project for codeacademy.
I'm using a button to "save" the user's input to an array so it can later be appended into the resume.
However, I'm failing at getting the data to "save" to the array. I've looked at similar questions here on stackoverflow and I cannot for the life of me figure out what I am doing wrong.
here's my fiddle
specific code block I'm having trouble with:
$('#experiencesave').click(function(){
for (var i = 0; i < jobs; i++){
jobtitle.push = $('#jobtitle'+i).val();
}
$('#morejobs').append(jobtitle);
});
Well, .push [MDN] is a function which has to be called:
jobtitle.push($('#jobtitle'+i).val());
As an alternative solution, instead of using a for loop, you might want to use .map to collect the values:
var jobtitle = $('input[id^=jobtitle]').map(function() {
return this.value;
}).get();
I don't see a reason to give each of those input elements an ID though. Just give them a class. That makes it a bit easier to bulk-process them later. E.g. the selector could then just be $('input.jobtitle').
I am returning data from an api using the getJSON method. The api is from last.fm, and here is the code I have below...
$.getJSON('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=ba2641c3bcc4d9e90ff25d4a7414f4ec&mbid='+lastresult1mbid+'&format=json', function (infodata) {
console.log(infodata);
var lastrel1 =(JSON.stringify(infodata.album.releasedate));
var releasedate1 = $.parseJSON(lastrel1);
var fmtracks =(JSON.stringify(infodata.album.tracks.track[0].name));
var tracks1 = $.parseJSON(fmtracks);
$('#lastfminfo1').append(""+releasedate1+"");
$('#tracks1').append(""+tracks1+"");
});
For the varible fmtracks there are 15 tracks in this album I need to display, so how can I output them all to the lastmininfo1 div?
Track[0].name will work, as will track[1].
I need a loop or [i] or a each function to display them all but everything I am trying will not work.
Help or a point in the write direction would be highly appreciated, Thanks in advance
$.getJSON('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=ba2641c3bcc4d9e90ff25d4a7414f4ec&mbid=d63d427d-499c-4eb4-996e-535e36013766&format=json', function (infodata){
console.log(infodata);
var trackList = infodata.album.tracks.track
for(var i = 0, currentTrack; currentTrack = trackList[i]; ++i){
$("#lastfminfo1").append(currentTrack.name);
}
})
This works, you just need to loop through the tracks and append any information you want from them. This just does the name without any tags.
JSFiddle: http://jsfiddle.net/klatzkaj/3Z884/1/
Yep, you'll have to loop over them, or when supporting IE<9 isn't an issue you could use the forEach method on tracks and push each title to an array.
Since you're using jQuery, however, you may want to look into $.map().