Array is not defined, yet I defined them as variables [duplicate] - javascript

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
I'm very new to Javascript, and I could use some help troubleshooting.
In the console log it says "upper" and "wordcount" are not defined. The goal of this function is to loop through an array derived from an inputdiv and ask if the array value is present in that array, AND is NOT present in the "wordcount" array, and if it's not, to push it in.
function processtext() {
var textindiv = document.getElementById("inputdiv").innerHTML;
var split = textindiv.split(" ");
var upper = [];
var wordcount = [];
for (var i = 0; i < split.length; ++i) {
upper.push(split[i].toUpperCase());
}
var sortedlist = upper.sort();
var wordcount = new Array;
for (var i = 0; i < sortedlist.length; ++i) {
if (sortedlist.indexOf(sortedlist[i]) > -1) {
if (wordcount.indexOf(sortedlist[i]) == -1) {
wordcount.push(sortedlist[i]);
}
}
}
}
console.log(upper);
console.log(wordcount);

The variables are defined in the function, not globally.
You may either move the console.log statements into the function, or move the variable declarations out of the function.

Related

How to save java script data from a loop to an array? [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Returning only certain properties from an array of objects in Javascript [duplicate]
(5 answers)
Javascript Array of objects get single value [duplicate]
(3 answers)
Construct an array of elements from an array of objects? [duplicate]
(2 answers)
Closed 4 years ago.
How to save javascript data from a loop to an array?
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
}
Insert the data in the array using push
var arr=[];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
arr.push(h);
}
var data = [];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
data.push(h)
}
//OR
var data = jsonData.Data.Positions.map(item => item.Oid);
Your variable jsonData.Data.Positions is probably already an array.
Use .push() method to add values to array.
var h=[];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
h.push(jsonData.Data.Positions[i].Oid);
}
console.log(h);
You can do it within a loop:
var array = []
for (i = 0; i < jsonData.Data.Positions.length; i++) {
array.push(jsonData.Data.Positions[i].Oid);
}
Or in a more functional-way:
var array = jsonData.Data.Positions.map(p => p.Oid)
map instead of for loop
var h = jsonData.Data.Positions.map(function (x) { return x.0id });

Arrays inside an array Javascript [duplicate]

This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
Closed 5 years ago.
I want to make an array so that it contains some identity name and for each of those names there is another array associated. My approach is like,
for (var i = 0; i < 10; i++) {
for (var x = 0; x < 5; x++) {
var data = someServiceCall(i, x);
var identityName = i + '-' + x;
myArray[identityName] = data;
}
}
after executing this i get something like,
[1-1: Array(8), 1-2: Array(10), 1-3: Array(10), 1-4: Array(10),.. etc]
the next time I call this function I need to check whether 1-1 exists and if yes I need to get the list related to 1-1. How can I do this..? if 1-1 is not in the myArray I will call some other function.
To check if the element having the 1-1 key exists just do:
if("1-1" in myArray)
Then to access the array associated with 1-1 use:
myArray["1-1"]
Try this. It inserts an object containing the identity name and data in each array element.
var myArray = [];
for (var i = 0; i < 10; i++) {
for (var x = 0; x < 5; x++) {
var data = someServiceCall(i, x);
var identityName = i + '-' + x;
var objectInArr = {
'identityName': identityName,
'data' : data
};
myArray.push(objectInArr);
};
};
try like this
myArray["1-1"] != undefined
Check if key exists in your array or not and act accordingly. Following is a working code snippet
var myArray = [];
for (var i = 0; i < 10; i++) {
for (var x = 0; x < 5; x++) {
var identityName = i + '-' + x;
myArray[identityName] = [1, 2];
}
}
var key = "0-0";
if(myArray[key])
console.log(myArray[key]);
you can check your array lenght and if the array is empty you will know that you need to call another action as you say. something like below might work for you
if (myArray.length === 0) {
//Call another function}
else {
//Somthing else}

Concatening variables in javascript [duplicate]

This question already has answers here:
How to convert string as object's field name in javascript
(5 answers)
Closed 7 years ago.
I have an array that I want to loop with a for.
for(var j = 0; j < outPutData.length; j++)
{
if(outPutData[j].pregunta1==1) { }
}
pregunta1, pregunta2, etc. are some of the variables.
If I print the value of outPutData[j].pregunta1 with an alert() it shows the correct value, but when I try:
for(var j = 0; j < outPutData.length; j++)
{
if(outPutData[j].pregunta+j==1) { }
OR
if(outPutData[j].pregunta+""+j==2) { }
}
an error is shown. Why?
What I expect to have is
outPutData[j].pregunta1
outPutData[j].pregunta2
outPutData[j].pregunta3
... (more till 200)...
outPutData[j].pregunta200
What am I doing wrong?
Thanks!
Try this:
outPutData[j]['pregunta' + j]
you can access properties of object by string using []:
var obj = {prop: "value"}
var a = obj.prop;
// is the same as
var b = obj['prop'];

Double Dimensional Array in Javascript Returning Undefined Array [duplicate]

This question already has answers here:
Javascript two dimensional arrays [duplicate]
(6 answers)
Closed 8 years ago.
I have searched alot and cannot get a clear answer to my problem.
var rowCount = 3;
var myCounter = 0;
var myNewArray = new Array();
for (var i = 1; i < rowCount; i++) {
try {
myNewArray[myCounter][0] = i;
myNewArray[myCounter][1] = i;
myCounter = myCounter + 1;
} catch (err) {
alert(err.message);
}
}
it is giving and exception saying myNewArray[myCounter] is undefined. Any idea why? I have seen other post and all have shown to declare the array like this or with new Array([]). Nothing is working.
Need help, in advance thank you.
Currently myNewArray is an array but the elements you are trying to access in it do not yet exist (undefined) so you would need to set these elements as arrays
var rowCount = 3;
var myCounter = 0;
var myNewArray = new Array();
for (var i = 1; i < rowCount; i++) {
try {
//set this element as an array if you want to then access it as an array
myNewArray[myCounter] = [];
myNewArray[myCounter][0] = i;
myNewArray[myCounter][1] = i;
myCounter = myCounter + 1;
} catch (err) {
alert(err.message);
}
}

Can a variable name determined by array value in a for-loop? [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 9 years ago.
Is it possible to use an array variable value as a new variable name in a for-loop (or in other words, can the left-hand side of an equation be determined by a pre-defined value in an iteration)?
for (var i = 0; i < array.length; i++) {
// we all know this is possible:
blabla[i] = apple;
// but I'm wondering if there's a way we can achieve this:
example(someVar);
}
function example(name) {
// [name] = banana;
name = banana;
}
Obviously, the way I'm doing this in the snippet above, the value banana is always getting assigned to the variable name. Not too sure though how I could go about this?
Variables are properties, either of the global object (in a browser: window) or of some other object. So you can do:
for (var i = 0; i < array.length; i++) {
window[array[i]] = '[something]';
}
or
var someObj = {};
for (var i = 0; i < array.length; i++) {
someObj[array[i]] = '[something]';
}
function myVar(val) {
this.value=val;
}
var array=new Array( new myVar(1),new myVar(2),new myVar(3));
for (var i = 0; i < array.length; i++) {
example(array[i]);
alert(array[i].value); // now they are 0,0,0
}
function example(name) {
name.value = 0;
}

Categories

Resources