'Cannot read property 'split' of undefined' for array [duplicate] - javascript

I have a problem with a javascript array: "arrFinal[i] is undefined"
In my script arrFinal is dynamically generated
function fillTextareas () {
var arrFinal = [];
arrFinal[0] = [];
....
....
// Then some code that define the content of arrFinal, the length of arrFinal ( tailleArrFinal, tailleArrSubFinal)
....
....
for(i=0;i<=tailleArrFinal;i++){
for(j=0;j<tailleArrSubFinal;j++) {
$("form textarea#t" + i + "_" + j).val(arrFinal[i][j]);
}
}
}
When the function is called, a dump show me that the array arrFinal is correctly fill and the script works but i have an alert "arrFinal[i] is undefined". How can i do that without alert ?
Thanks !!

It looks like an off-by-one error in the outer loop.
It should be i < tailleArrFinal, not <=.

By looking at your loop I can see at least 2 errors: you miss "var" and ".length" (you have to test for array length!)
try to replace:
for(i=0;i<=tailleArrFinal;i++){
for(j=0;j<tailleArrSubFinal;j++) {
$("form textarea#t" + i + "_" + j).val(arrFinal[i][j]);
}
}
}
with:
for(var i=0;i<tailleArrFinal.length;i++){
for(var j=0; j<tailleArrSubFinal.length; j++) {
$("form textarea#t" + i + "_" + j).val(arrFinal[i][j]);
}
}
}

Related

Is it possible to change default fallback message i18n-js?

I want to change fallback message on missing translation for i18n-js ([missing translation "en....."]) Is it possible and how to?
Thanks #Vasfed, but now i know easier way to do this.
Just add:
I18n.t("some.missing.scope", {defaults: [{message: "Some message"}]});
Instead of [missing translation "bla.bla.bla" ] you will get "Some message".
In library itself it is defined this way:
I18n.missingTranslation = function() {
var message = '[missing "' + this.currentLocale()
, count = arguments.length
;
for (var i = 0; i < count; i++) {
message += "." + arguments[i];
}
message += '" translation]';
return message;
};
you can replace this with your own implementation by reassigning I18n.missingTranslation after library is already evaluated.

Uncaught SyntaxError: missing ) after argument list Error - Creating HTML Javascript Function Call

I'm trying to make a sidebar by copying code and editing it. I've moved the myclick function before sidebar, I've checked for missing {'s, I've tried various forms of
html += [code] and I continually get this error. When I try it with what I have below, I get
"Bareburger<br>Shake Shack<br>"
in the alert(html) which gives me "Uncaught SyntaxError: Unexpected end of input. I have also tried switching the html line with
html += '' + uniquerestaurants[i] + '<br>';
except this part gives me in the alert:
Bareburger<br>Shake Shack<br>
and this error: "Uncaught SyntaxError: missing ) after argument list ". I'm completely stuck because I copied this html += line from something that works and made the appropriate changes and still have yet to get the call to myclick to work properly. I've run out of things to guess...please help.
Don't know if this matters, but I've also tried removing the semicolon in ');">' and changing the order of the functions, but neither helped.
function makeSidebar() {
var html = "";
restaurantsbytype = [];
uniquerestaurants = [];
//for all markers clicked, get the restaurant name and make it a unique array
for (var i=0; i<markerarray.length; i++) {
if (markerarray[i].getVisible()) {
restaurantsbytype.push(markerarray[i].myname);
}
}
uniquerestaurants = restaurantsbytype.unique();
//for unique list of restaurant names, create href
for (var i=0; i<uniquerestaurants.length; i++) {
html += '' + uniquerestaurants[i] + '<br>';
}
alert(html);
document.getElementById("side_bar").innerHTML = html ;
}
function myclick(name) {
alert(name);
for (var i=0; i<markerarray.length; i++) {
if (markerarray[i].myname == name) {
//make markers of selected category bounce for 2.5 seconds
markerarray[i].setAnimation(google.maps.Animation.BOUNCE);
stopAnimation(markerarray[i]);
}
}
}
Try something like:
html += '' + uniquerestaurants[i] + '<br>';

Detecting if input has been entered before

Lately I've been having trouble checking whether an input (player name) has been input more than once. This is not in-database, but just based on arrays contained within JavaScript. What I've been using after a couple of google searches was the indexOf() function. However this does not seem to work. My code is as follows:
var numberPlayers = 1;
var players = [];
var AddPlayer = function() {
if(!(players.indexOf($(".name")).val() > -1)) {
players.push($(".name").val());
$(".players").append("<p>Player number " + numberPlayers + " is " + $(".name").val() + "</p>");
numberPlayers++;
}
};
What method of detection would you recommend? I've tried looping, but wouldn't work either.
EDIT: Updated code. Still doesn't work!
Try this - note where the ) is placed:
if(!(players.indexOf($(".name").val()) > -1)) {
instead of:
if(!(players.indexOf($(".name")).val() > -1)) {
and actually, for readability this would be better:
var name = $('.name').val();
if ( players.indexOf(name) == -1)) {
In general, try adding console.log and breakpoints to find your bugs...
You can use an object (read Set / Hash) instead of an array; it should be faster anyway.
Note that I'm also using .text() which will escape text.
var numberPlayers = 1;
var players = {};
var AddPlayer = function() {
var newPlayer = $(".name").val();
if(!(newPlayer in players)) {
players[newPlayer] = true;
$(".players").append($("<p>").text("Player number " + numberPlayers + " is " + newPlayer));
numberPlayers++;
}
};
jQuery has a utility function for this:
$.inArray(value, array)
It returns the index of a value in an array. It returns -1 if the array does not contain the value.

JavaScript splice() not working

for(var i = 0; i < this.phrases.length; i++) {
console.log('sort ' + this.phrases[i]);
console.log('sort length' + this.phrases.length);
if(this.phrases[i] == null) {
var s = this.phrases.splice(i, 1);
console.log('splice ' + this.phrases[i]);
console.log('splice length ' + this.phrases.length);
}
}
I have an array (this.phrases). I made the phrase that I want to remove equal to null in another section. The first log prints null, the second log also prints null. Why is it not getting spliced? This is also sometimes the last item in an array. Are you not able to splice an array with only one element? The same thing happens, however if it is not the last item in this.phrases.
Edit: s also does not seem to get set to any value.
Edit: The two length logs I added print the same number.
Edit: That's not actually true. Weird things are happening with the lengths, probably not involving this section of code. I just want to know if I'm using splice() correctly.
Switch these two lines:
var s = this.phrases.splice(i,1);
console.log('splice ' + this.phrases[i]);
To this:
console.log('splice ' + this.phrases[i]);
this.phrases.splice(i, 1);
i--;
const indexOfValue = treeSelectDataTemp.findIndex((item: any) => item.value === deleteIndexId[d]);
console.log(" ########### deleted indexOfValue ", indexOfValue);
if (indexOfValue !== -1) {
treeSelectDataTemp.splice(indexOfValue, 1);
}

Javascript Error: Cannot Convert Object to Primitive Value

I'm receiving this error using the following javascript code:
function tempTest(evt) {
alert(evt.currentTarget.id);
ct = document.getElementById(evt.currentTarget.id);
rslt = document.getElementById('rslt');
var props;
for (var prop in ct) {
if (ct.hasOwnProperty(prop)) {
propVal = ct[prop];
var propDat = prop + ' = ' + propVal;
props += propDat + '<br/>';
}
}
rslt.innerHTML = props;
}
This one has me puzzled. Any ideas?
Not all the properties of a HTML element are primitives. for example, parent, childs etc are also HTML elements. You can't just use them as strings or numbers.
You need to add there a condition and use that property accordingly.
If the object in question is json, you can call JSON.stringify(thingThatIsJson) which will return a String. .toString() does not work on json.
This is a message to those of you dealing with something like req.body which will work in console.log() which is rather confusing since it may not otherwise behave like a String (like when you're trying to add it to another String).
(The OP:)
Just wanted to post the updated snippet for anyone who stumbles onto this post...
function tempTest(evt) {
alert(evt.currentTarget.id);
ct = document.getElementById(evt.currentTarget.id);
rslt = document.getElementById('rslt');
var props;
for (var prop in ct) {
if (ct.hasOwnProperty(prop)) {
var propVal = ct[prop];
props += prop + ' (' + typeof(prop) + ')' + ' = ';
if (typeof(ct[prop]) == 'string') {
propVal += ct[prop];
} else {
if (propVal != null && propVal.toString) {
props += propVal.toString();
} else {}
}
props += '<br/>';
}
}
rslt.innerHTML = props;
}
The problem lies with the propVal part of your code. Since that may not be converted into a string.

Categories

Resources