Put JSON data into HTML elements - html

I want to put values from a JSON object into HTML elements.
If I break this down into steps, I think I know the first two steps, but then I am lost
get the html elements into javascript objects using a loop and selectors
get the values of the JSON properties one by one using a for...in loop
...
I am already at a stumbling block in step 1, as I don't know how to select by ID when iterating over the set of elements.
I would like help to understand the steps I need to define for this task, as well as any advice about which functions to use.
This is the JSON :
var sonnet_1 = {
line_1 : "FROM fairest creatures we desire increase",
line_2 : "That thereby beauty\'s rose might never die",
line_3 : "But as the riper should by time decease,",
line_4 : "His tender heir might bear his memory\:", }
Here is the HTML:
<p id = "1"></p>
<p id = "2"></p>
<p id = "3"></p>
<p id = "4"></p>
Step 1 ) I have tried this
First, tested a single query
document.getElementById('1');
OK. output: <p id ="1"></p>
for (i= 0; i< 14 ; i++) {
var line = document.getELementById('i');
console.log(line);
}
I am expecting output:
<p id ="1"></p>
<p id ="2"></p>
...
But instead this doesn't log anything.
Step 2)
Tried this :
console.log(`${sonnet_1[line]}`);
Output: FROM fairest creatures we desire increase
All good!
Then this:
for (line in sonnet_1) {
console.log(`${sonnet_1[line]}`);
}
Again all good.
Output:
FROM fairest creatures we desire increase
That thereby beauty's rose might never die
But as the riper should by time decease,
His tender heir might bear his memory:
Then this:
for (line in sonnet_1) {
document.getElementById(`${line}`).innerHTML = `${sonnet_1[line]}`;
}
No luck.
I think I need an embedded loop. Is this correct?

Before going to js put a div with id, lets id="div";
let x;
for (i in sonnet_1) {
x += '<p>' + sonnet_1[i] + '</p>';
}
document.getElementById("div").innerHTML = x;

You are on the right direction.
let i = 1;
for (line in sonnet_1) {
// console.log(i++, sonnet_1[line]);
document.getElementById(`${i++}`).innerHTML = `${sonnet_1[line]}`;
}
If you want to create dynamic list of 'p' elements over the JSON object, try this:
let i = 1;
let htmlStr = '';
for (line in sonnet_1) {
htmlStr = htmlStr + `<p id='${i}'>${sonnet_1[line]}</p>`
}
console.log(htmlStr);
DEMO

Related

Angular: Posting Data to a Table with a loop and giving a problem when posting the i of the loop into the table

Good day,
I am trying to get data into a table, with the tour_id and every single media_id (the station_id i am getting from somewhere else), the ordernumber is what is giving me a headache:
I am trying to get every station one number for every media i am posting.
For example:
station 1 has 2 medias
and station 2 has 3
then the odernumbers should be like this: 0, 0, 1, 1, 1
I am using the following Code at this moment:
for(var i = 0; i < this.currentStations.length; i++){
this.http.get("http://localhost:3000/mediasforstation/" + this.currentStations[i].id).subscribe((res) => {
medias = res;
for (var j = 0; j < medias.length; j++){
this.http.post("http://localhost:3000/posttourstations",
{"tour_id": id, "media_id": medias[j].id, "ordernumber": i}).subscribe(function(res) {
console.log(res);
}.bind(this));
}
});
}
Everything but the ordernumber works, however, the ordernumber always takes the number of stations involved, in our example above it would be 2.
How do I fix this?
Thank you very much for your help.
As I understand, you need to keep the index value. The type of variable i is var which is function scoped. Within outer loop, you are calling an API that returns some response, meanwhile the value of i is updated and for next index/counter, the API call has been sent. When you get response from API calls, you get the value of i where the outer loop has been called of.
In other words, you need to understand the difference between var and let. Your problem can be solved by replacing
for(var i=0;...)
with
for(let i=0;...)
Here's providing you the sample code.
//block scoped - retains value of i
for (let i=0;i<10;i++){
this.http.get('https://jsonplaceholder.typicode.com/users').subscribe(res=>{
for(var j=0;j<5;j++){
console.log(`i=>${i}`)
}
})
}
//function scoped - gets updated value of i
for (var i=0;i<10;i++){
http.get('https://jsonplaceholder.typicode.com/users').subscribe(res=>{
for(var j=0;j<5;j++){
console.log(`i=>${i}`)
}
})
}

Trouble Adding Array output to an Dynamically Generated HTML String in GAS Google Script

I am trying to automate my businesses blog. I want to create a dynamic html string to use as a wordpress blog description. I am pulling text data from email body's in my gmail account to use as information. I parse the email body using the first function below.
I have everything working properly except for the for loop (in the second code block) creating the description of the post. I have searched for hours and tried dozens of different techniques but I cant figure it out for the life of me.
Here is how I am reading the text values into an array:
function getMatches(string, regex, index) {
index || (index = 1); // default to the first capturing group
var matches = [];
var match;
while (match = regex.exec(string)) {
matches.push(match[index]);
}
return matches;
}
This is how I am trying to dynamically output the text arrays to create a basic HTML blogpost description (which I pass to xmlrpc to post):
var1 = getMatches(string, regex expression, 1);
var2 = getMatches(string, regex expression, 1);
var3 = getMatches(string, regex expression, 1);
var3 = getMatches(string, regex expression, 1);
var fulldesc = "<center>";
var text = "";
for (var k=0; k<var1.length; k++) {
text = "<u><b>Var 1:</u></b> " + var1[k] + ", <u><b>Var 2:</u></b> " + var2[k] + ", <u><b>Var 3:</u></b> " + var3[k] + ", <u><b>Var 4:</u></b> " + var4[k] + ", <br><br>";
fulldesc += text;
}
fulldesc += "</center>";
Lastly here is the blog post description code (using GAS XMLRPC library):
var fullBlog = "<b><u>Headline:</u> " + sub + "</b><br><br>" + fulldesc + "<br><br>General Description: " + desc;
var blogPost = {
post_type: 'post',
post_status: 'publish', // Set to draft or publish
title: 'Ticker: ' + sub, //sub is from gmail subject and works fine
categories: cat, //cat is defined elsewhere and works fine
date_created_gmt: pubdate2, //defined elsewhere (not working but thats another topic)
mt_allow_comments: 'closed',
description: fullBlog
};
request.addParam(blogPost);
If there's only one value in the var1,2,3,4 arrays all works as it should. But any more than 1 value and I get no output at all from the "fulldesc" var. All other text variables work as they should and the blog still gets posted (just minus some very important information). I'm pretty sure the problem lies in my for loop which adds the HTML description to text var.
Any suggestions would be greatly appreciated, I'm burned out trying to get the answer! I am a self taught programmer (just from reading this forum) so please go easy on me if I missed something stupid :)
Figured it out: It wasnt the html/text loop at all. My blogpost title had to be a variable or text, but not both.
Not working:
title: 'Ticker: ' + sub, //sub is from gmail subject and works fine
Working:
var test = 'Ticker: ' + sub;
//
title:test,

How to merge the contents of two variables

I have many string variables that start with "Question" and then end with a number. ("Question1")
Each variable has a question in it ("How many times does it say E?")
There is an editable textbox on the stage that the user types in which question number he want to be displayed in a different textbox. ("1")
When the user clicks a button, I want that the text of Question1 should be displayed in the textbox.
My code looks like this:
var Question1:String = "How many times does it say E?" ;
var Question2:String = "How many times does it say B?" ;
var Question3:String = "How many times does it say A?" ;
myButton.addEventListener(MouseEvent.CLICK, displayQuestion);
function displayQuestion(event:MouseEvent):void
{
var QuestionNumber:Number = Number(userInputQuestionNumber.text);
textBoxDisplayQuestion.text= Question(QuestionNumber);
}
How can I get the textBoxDisplayQuestion to display the actual text of the Question??
(the code i have now obviously is not working!!)
But this example doesnt seem to work: I created a class called Question and here is the code:
import Question;
var QuNoLoad:Number;
var Qu1:Question = new Question(1,"how","yes","no","maybe","so","AnsB","AnsA");
trace(Qu1.QuNo, Qu1.Qu, Qu1.AnsA,Qu1.AnsB, Qu1.AnsC, Qu1.AnsD, Qu1.CorAns, Qu1.FaCorAns);
//the following is the code for the button
loadQu.addEventListener(MouseEvent.CLICK, loadQuClick);
function loadQuClick(event:MouseEvent):void
{
//this sets the variable "QuNoLoad" with the contents of the "textBoxQuLoad"
//imagine the user inputed "1"
QuNoLoad=Number(textBoxQuLoad.text);
//this SHOULD!! display the contents of "Qu1.Qu"
textQu.text= this["Qu"+QuNoLoad.toString()+".Qu"]
//and when i traced this statment the value was "undefined"
}
Why???
You can reference a variable by name using square brackets [] operator, such as:
this["Question" + QuestionNumber.toString()]
You may use this operator to dynamically set and retrieve values for a property of an object.
Keeping the question number as an integer, your function would be:
var Question1:String = "How many times does it say E?" ;
var Question2:String = "How many times does it say B?" ;
var Question3:String = "How many times does it say A?" ;
function displayQuestion(event:MouseEvent):void
{
var QuestionNumber:uint = uint(userInputQuestionNumber.text);
textBoxDisplayQuestion.text = this["Question" + QuestionNumber.toString()];
}
This is a pretty fundamental concept in programming that will make a lot of things harder to do until you understand it well, and it's pretty hard to explain without starting with some groundwork:
What's happening here is easiest to talk about with plain old Object rather than classes, so lets start with a very simple example:
var question1:Object = new Object();
question1.number = 1;
Note that with Object you didn't have to say that number existed ahead of time, it gets created when you set it. Now, when you say either question1.number you get 1, obviously. What is happening, however is that first question1 gets the value you stored in the variable question1 (which is { number: 1 }), then the .number gets the value stored in the property number stored in that value: 1.
To save some typing, you can use a shorthand called "object literals":
var question1 = {
number: 1
};
Now lets try a more complex object:
var question1 = {
number: 1,
text: "How many times does it say A?",
answers: {
a: 1,
b: 2,
c: 3,
d: 4,
correct: "b"
}
};
Now question1 is an object that has 3 properties, one of which, answers, is an object with 5 properties: a, b, c, d, and correct. This could also be written as:
var question1 = new Object();
question1.number = 1;
question1.text = "How many times does it say A?";
question1.answers = new Object();
question1.answers.a = 1;
question1.answers.b = 2;
question1.answers.c = 3;
question1.answers.d = 4;
question1.answers.correct = "b";
It should be pretty clear why the literal syntax exists now!
This time, if you say question1.answers.correct you get "b": first question1 gets you the { number: 1,...} value, then the .answers gets the { a: 1, b: 2,...} value, then finally the .correct gets the "b" value.
You should also know that this is a special variable that has a particular meaning in ActionScript (and JavaScript, on which it is based): it broadly refers to the object in when the code you are writing is inside: for "global" code (not inside a function), var adds properties to this object: var number = 2; and this.number = 2 are this same here. (This is not true when you're in function, this behaves differently there, sometimes in very strange ways, so be careful!)
Now you might start seeing what's happening: when you use [], for example, question1["number"], rather than question1.number, you are passing the property name you want to get as a String value, which means you can change what property you get while you are running, rather than when you compile ("runtime" vs. "compiletime"), but it also lets you get properties with names you can't refer to with the . syntax!
var strange = {
"a strange name? That's OK!": 1
};
trace(strange["a strange name? That's OK!"]);
So when you write this["Qu" + QuNoLoad.toString() + ".QuNo"], you create a name like "Qu2.QuNo", for example, you are trying to get a property with that exact name, . included, which doesn't exist! What you were trying to do the equivalent of: Qu2.QuNo could be written as this["Qu" + QuNoLoad].QuNo.
I shouldn't leave this without saying, though, that for something like this, I would use arrays, which exist so that you can use a single name to store a list of values:
var questions:Array = [ // set questions to an array with multiple questions
new Question(...),
new Question(...),
...
];
for each (var question:Question in questions) { // Look at each question in the array
if (question.QuNo == textBoxQuLoad.text) { // If this is the right question
loadQuestion(question);
break; // Found it, stop looking at each question by "breaking out" of the for each
}
}
There's lots more you can do with arrays, so read up on them when you get time.

How to find specific value in a large object in node.js?

Actually I've parsed a website using htmlparser and I would like to find a specific value inside the parsed object, for example, a string "$199", and keep tracking that element(by periodic parsing) to see the value is still "$199" or has changed.
And after some painful stupid searching using my eyes, I found the that string is located at somewhere like this:
price = handler.dom[3].children[3].children[3].children[5].children[1].
children[3].children[3].children[5].children[0].children[0].raw;
So I'd like to know whether there are methods which are less painful? Thanks!
A tree based recursive search would probably be easiest to get the node you're interested in.
I've not used htmlparser and the documentation seems a little thin, so this is just an example to get you started and is not tested:
function getElement(el,val) {
if (el.children && el.children.length > 0) {
for (var i = 0, l = el.children.length; i<l; i++) {
var r = getElement(el.children[i],val);
if (r) return r;
}
} else {
if (el.raw == val) {
return el;
}
}
return null;
}
Call getElement(handler.dom[3],'$199') and it'll go through all the children recursively until it finds an element without an children and then compares it's raw value with '$199'. Note this is a straight comparison, you might want to swap this for a regexp or similar?

Re-stacking MovieClips in an Array

I was trying to make a similar thing with the game SameGame (ie. the block above the removed blocks fall downward). Before trying this with an Array that contains MovieClips, this code worked (tried it with int values). With MovieClips on the array, it seems not working the same way.
With int values, example:
popUp(0, 4): Before: 1,2,3,4,5,6,7,8,9,10; After: 1,2,3,4,6,7,8,9,10
But with MovieClips:
popUp(0, 4): Before: 1,2,3,4,5,6,7,8,9,10; After; 1,2,3,4
// Assume the numbers are movieclips XD
Basically, it strips everything else, rather than just the said block >_<
Here's the whole method. Basically, two extra arrays juggle the values above the soon-to-be removed value, remove the value, then re-stack it to the original array.
What could be wrong with this? And am I doing the right thing for what I really wanted to emulate?
function popUp(col:uint, row:uint)
{
var tempStack:Array = new Array();
var extraStack:Array = new Array();
tempStack = IndexArray[col];
removeChild(tempStack[0]);
for(var ctr:uint = tempStack.length-(row+1); ctr > 0; ctr--)
{
removeChild(tempStack[ctr]);
extraStack.push(tempStack.pop());
trace(extraStack);
}
tempStack.pop();
for(ctr = extraStack.length; ctr > 0; ctr--)
{
tempStack.push(extraStack.pop());
//addChild(tempStack[ctr]);
}
IndexArray[col] = tempStack;
}
PS: If it's not too much to ask, are there free step-by-step guides on making a SameGame in AS3 (I fear I might not be doing things right)? Thanks in advance =)
I think you just want to remove an element and have everything after that index shift down a place to fill what you removed. There's an inbuilt function for this called splice(start:uint, length:uint);
Parameters:
start - the index to start removing elements from
length - the amount of elements to remove
var ar:Array = ["hello","there","sir"];
ar.splice(1, 1);
ar is now -> ["hello", "sir"];
As per question:
Here's an example with different types of elements:
var ar:Array = [new MovieClip(), "some string", new Sprite(), 8];
ar.splice(2, 1);
trace(ar); // [object MovieClip], some string, 8
And further example to display the indexes being changed:
trace(ar[2]); // was [object Sprite], is now 8