Best method for calling function parameters in a for loop? - javascript

So I'm working with some slightly tricky code. Basically, I'm trying to pull a <script> tag from around 10 pages on a site I'm developing for. This code's syntax is incorrect, as you cannot use brackets in function parameters, but it's the essence of what I'm trying to perform:
var parser = new DOMParser();
var resp = new Array();
var htmlDoc = new Array();
var findScripts = new Array();
var searchScripts = new Array();
var scriptContent = new Array();
for (var i = 0; i < amt; i++) {
resp[i] = prodDetails[i].responseText;
htmlDoc[i] = parser.parseFromString(resp[i],"text/html");
findScripts[i] = htmlDoc[i].body.querySelectorAll('script');
searchScripts[i] = Array.prototype.filter.call(findScripts[i], function (findScripts[i]) {
return RegExp('var prodInfo = ').test(findScripts[i].textContent);
});
scriptContent[i] = searchScripts[i].innerText;
}
Further, possibly unneeded details:
I'm using the following code to grab each page:
var text = "";
var prodDetails = new Array();
var amt = document.querySelectorAll('[id="product-details"]').length;
for (var i = 0; i < amt; i++) {
prodDetails[i] = $.get(itemPages[i].href, {}, function (results) {
});
}
Following this, I am then parsing the information so that the tag can be pulled through simple JavaScript commands:
var parser = new DOMParser();
var resp = new Array();
var htmlDoc = new Array();
for (var i = 0; i < amt; i++) {
resp[i] = prodDetails[i].responseText;
htmlDoc[i] = parser.parseFromString(resp[i],"text/html");
}
This works for accessing each page's DOM individually by calling htmlDoc[0] through htmlDoc[9], but there are around 8 <script> tags on each of the pages. The one that I'm looking for contains specific text in its innerHTML. I can find the one I'm looking for using:
var findScripts = htmlDoc[0].body.querySelectorAll('script');
var searchScripts = Array.prototype.filter.call(findScripts, function (findScripts) {
return RegExp('var prodInfo = ').test(findScripts.textContent);
});
var scriptContent = searchScripts[0].innerText;
This code works great when ran on its own, but this means manually running each time changing the index value of htmlDoc, and I'm looking for more of an "all at once" solution.
I'm not opposed to using jQuery in this, but I am mostly unfamiliar with it. If there is a more powerful jQuery-based solution, I will take that as well. Any help is appreciated! Thank you!

Rather than assigning to someArr[i] and someOtherArr[i] over and over, I suggest just pushing to the arrays in question instead, the code will look a lot cleaner. Also, you can use forEach for better abstraction, to avoid manual iteration, and to avoid the problems with i hoisting:
const parser = new DOMParser();
const responseTexts = [];
const htmlDocs = [];
const findScripts = [];
const filteredScripts = [];
const scriptContent = [];
prodDetails.forEach((response) => {
const responseText = response.responseText;
const newDoc = parser.parseFromString(responseText,"text/html");
const thisDocScripts = newDoc.querySelectorAll('script');
const thisDocFilteredScripts = [...thisDocScripts]
.filter(oneScript => oneScript.textContent.includes('const prodInfo = '));
const thisDocScriptsContent = thisDocFilteredScripts.map(scr => scr.textContent);
responseTexts.push(responseText);
htmlDocs.push(newDoc);
findScripts.push(thisDocScripts);
filteredScripts.push(thisDocFilteredScripts);
scriptContent.push(thisDocScriptsContent);
});
Abstraction is wonderful.
Do you really need all of those variables saved in arrays, though?

Related

Javascript - Help retrieving data from Firebase

I have this code below to retrive data from firebase and turn into an array but whenever i call it it stops other functions from being called inside the code even tho I can perfectly call them in the console.
I've worked around this by using callback functions but I am at a point it just doesn't do anymore. How can make this part act like a normal function where I can call anytime without stopping the code.
function genList(){
dataRef.on('value', data => {
let arr = (data.val());
let keys = Object.keys(arr);
for (let i = 0; i < keys.length; i++) {
let k = keys[i];
let title = arr[k].title;
let author = arr[k].author;
let pages = arr[k].pages;
let date = arr[k].date;
let bookmark = arr[k].bookmark;
let newList = new Object();
newList.title = title;
newList.author = author;
newList.pages = pages;
newList.date = date;
newList.bookmark = bookmark;
bookList.push(newList);
}
})}

Changing one variable changes all others defined the same way

I have a JavaScript code which has 4 3-dimensional arrays that are each of 500x500x220 dimension (all 220 values in the last dimension are rarely all used). Because of this large dimension, it's much faster to define one array like this and then define the four arrays from that one. The problem is that then, when I change a value in one array, it changes in the others also. Here's my code:
<script type="text/javascript">
var content = new Array();
var signs = new Array();
var sens = new Array();
var props = new Array();
var ini = new Array();
for(i = 0; i < 500; i++){
ini[i] = new Array();
for(j = 0; j < 500; j++){
ini[i][j] = new Array();
}
}
content = ini;
signs = ini;
sens = ini;
props = ini;
function f(){
alert(signs[3][3][2]); //Returns undefined
content[3][3][2] = 2;
alert(signs[3][3][2]); //Returns 2
}
f();
</script>
Notice that the f() function is only supposed to change the content array but it also changes the signs array. Why does it do that and how do I get around it?
In case it makes a difference, I'm using HTA.
With the help of this post about copying nested arrays.
Your code:
content = ini;
signs = ini;
sens = ini;
props = ini;
makes the arrays to point to ini. That's why any reference to content[0], for instance, is a reference to signs[0] and ini[0] as well.
Use:
function copy(arr){
var new_arr = arr.slice(0);
for(var i = new_arr.length; i--;)
if(new_arr[i] instanceof Array)
new_arr[i] = copy(new_arr[i]);
return new_arr;
}
to copy the arrays:
content = copy(ini);
signs = copy(ini);
sens = copy(ini);
props = copy(ini);

Issue with javascript array variables

I am using javascript in my app which is a rhino javascript engine. I like to use array for an array.
The following code is my actual working code.
while (input.hasNext()) {
var data = input.next();
var new_data = {};
var i = 0;
while(i<data.get('total_entries')){
new_data.entries = data.get('total_entries');
new_data.id = data.get('out').get(i).get('id');
output.write(new_data);
i++;
}
}
I need to create array for new_data[].
while (input.hasNext()) {
var data = input.next();
var new_data[] = {};
var i = 0;
while(i<data.get('total_entries')){
new_data[i].entries = data.get('total_entries');
new_data[i].id = data.get('out').get(i).get('id');
output.write(new_data[i]);
i++;
}
}
But its not working. So, help me to create array variable in it.
Thanks.
You mean something like this:
while (input.hasNext()) {
var data = input.next();
var new_data = [];
var i = 0;
while(i<data.get('total_entries')){
new_data.push({entries: data.get('total_entries'), id: data.get('out').get(i).get('id')});
output.write(new_data[i]);
i++;
}
}
var new_data[] = {};
First of all, that's invalid JavaScript.
new_data[i]
Looks like you want new_data to be an array of objects. Why not declare it as one and add objects on every iteration of the while loop:
var new_data = [];
while(...){
new_data.push({
entries: ...
id: ...
});
}

How to parse bracket tag on Javascript

I have tag like this, how the best way to get every key and value of those attribute and populate it within an array (number of attribute will be increasing)?
myData = '[data attr1="value1" attr2="value2" attr3="value3"]';
and get result array :
var arr = new Array();
arr['attr1'] = "value1";
arr['attr2'] = "value2";
arr['attr3'] = "value3";
and so on...
This probably does what you want, though it assumes that tag is already in the format you have described, i.e. a singular occurrence of [data ... ].
Also, the regular expression is purely based on what I've seen in your question; not sure whether it will break on other strings.
function decode(tag)
{
var r = /(\w+)="([^"]*)"/g,
h = {};
while ((m = r.exec(tag)) !== null) {
h[m[1]] = m[2];
}
return h;
}
Since you have string key in the data, use jquery object instead of array.
var arr = {};
var str = '[data attr1="value1" attr2="value2" attr3="value3"]​​​';
var n = str.split('[data ');
var str_arr = n[1].replace(']','').split(" ");
jQuery.each(str_arr,function(val){
var x = str_arr[val].split('=');
arr[x[0]] = x[1].replace('"','').slice(0,-1);
});
console.log(arr);
Try this code. It may help you.
Here is the DEMO
Though it can be more optimized if you put some more details about your code.
var tagRe = /\[(\w+)((?:\s+\w+="[^"]{0,50}")*)\s*]/g;
var attrRe = /\b(\w+)="([^"]*)"/g;
function parse(text) {
var result = [];
tagRe.lastIndex = 0; // reset start position
var tagMatch = tagRe.exec(text);
while (tagMatch) {
var currentTag = { 'name': tagMatch[1], 'attrs': {} };
var attrString = tagMatch[2];
attrRe.lastIndex = 0;
var attrMatch = attrRe.exec(attrString);
while (attrMatch) {
var attrName = attrMatch[1];
var attrValue = attrMatch[2];
currentTag.attrs[attrName] = attrValue;
attrMatch = attrRe.exec(attrString); // next match
}
result.push(currentTag);
tagMatch = tagRe.exec(text);
}
return result;
}
parse('[data attr1="value1" attr2="value2" attr3="value3"]');
> [{name:'data',attrs:{attr1:'value1',attr2:'value2',attr3:'value3'}}]
This works for any number of tags in the string. The name of the tag does not matter.

What's wrong with this javascript? Array not defined

What's wrong with this code?
var divarray = document.getElementById("yui-main").getElementsByTagName("div");
var articleHTML = array();
var absHTML;
var keyHTML;
var bodyHTML = array();
var i = 0;
for ( var j in divarray) {
if(divarray[i].className == "articleBody"){
alert("found");
articleHTML = divarray[i];
break;
}
bodyHTML[i] = '';
if(articleHTML[i].className == "issueMiniFeature"){continue;}
if(articleHTML[i].className == "abstract"){absHTML = articleHTML[i]; continue;}
if(articleHTML[i].className == "journalKeywords"){keyHTML = articleHTML[i]; continue;}
bodyHTML[i] = articleHTML[i];
i++;
}
This is the error I am getting:
ReferenceError: array is not defined
I am using Google Chrome if it helps any.
It's not php - you should use
var variable_name = new Array()
or even better
var variable_name = []
That's not how to declare variables as an empty array. You should be using:
var articleHTML = [];
See this previous question for reasoning of using this method instead of new Array()
It's [] in ECMAScript; this isn't PHP. The interpreter is right - array is not defined, which is why you're getting that.
var articleHTML = new Array();
Note! Javascript IS case sensitive you have to use upper-case A in word Array.
var myarr = new array(); //THIS IS WRONG! and will result in error not defined
So these are the correct ways:
var myarr = new Array(); //THIS IS CORRECT (note the "big" A) :)
var myarr = []; //and this is correct too
Instead of
var articleHTML = array();
and
var bodyHTML = array();
do
var articleHTML = [];
and
var bodyHTML = [];
You first need to define
var divarray = new Array();
You also don't need to use var six times, you can do:
var divarray = document.getElementById("yui-main").getElementsByTagName("div"),
articleHTML = [],
absHTML = [],
keyHTML = [],
bodyHTML = [],
i = 0;
Which works just as well as your six vars but looks much nicer.
Also there are a number of compelling reasons not to use new in instantiate an array (besides []; is much shorter than new Array();)

Categories

Resources