Defining String Arrarys [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Hi I'm a beginner in Javascript and somethinh strange occured.
I wanted to create an global String array in an external Javascript file with
var natArray = new Array(20); But my IDE(Webstorm 7) Says me newArray is an Unresolved function and I cant use the array in another function it was created for(Source: Javascript Console)
Thank you for your time and feedback.
EDIT: Rest of the Code
function country(name){
for(i=0;i<20;i++)
natArray[i]=name;
document.write(natArray[i]);
}
And here i call it(a html file):
Country:
<select>
<script type="text/javascript">
country("RandomCountry1");
country("RandomCountry2");
</script>
</select>
Im just getting an empty drop-down-list

I'm not sure what you are trying to accomplish, but the reason that document.write(natArray[i]); is returning undefined is that you are leaking i to the global scope in for(i=0;i<20;i++).
Once this loop has completed, i is equal to 20, even outside the scope of the loop (to avoid this, try for(var i=0;i<20;i++). Although natArray has a .length of 20, arrays elements are zero-indexed, meaning that they start counting at 0, not 1.
Because of this, if what you are trying to do is access the last element of natArray, you can/should do so with natArray[natArray.length - 1]. The -1 accounts for the zero-indexed nature of Javascript arrays. If natArray has a length of 20, you will fail to access natArray[20] because this statement is actually trying to access the 21st element, not the 20th.

Related

Pushing data to an array inside another array with Javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I made an array for a raffle system using node.js and discord.js which has this data set.
[{"author":"Name","rafflename":"RAFFLE","amount":"10","entries":"[]"}]
When someone tries to enter the raffle I'd like to make another JSON array inside the entries tag.
The problem i face is when i use raffles[0].entries.push({ 'username': 'example', 'user_id': '1' });
It returns an error: raffles[0].entries.push is not a function
I assume this is because it is looking for the array raffles[0].entries.push which does not exist. But I've only ever used the push command. So I am not sure how to fix this issue.
Move your entries [] out of double quotes, i.e. Use "entries" :[]
You are trying to push to a string instead of array
You have to cast entries to Array first.
const raffles = [{"author":"Name","rafflename":"RAFFLE","amount":"10","entries":"[]"}]
// cast entries to Array
raffles[0].entries = JSON.parse(raffles[0].entries)
// now you can push them
raffles[0].entries.push('test')
console.log(raffles)
It isn't working because you have "" around the properties inside of your object. you can fix this by either removing the "" or doing this:
raffles[0]['entries'].push(//your code here);
When properties of an object are strings (i.e. when they have quotes around them) you must access that property using "dot notation"
eg:
var object = {"property":"five"};
console.log(object["property"]); //prints 'five'
console.log(object.property); //throws an error
Format Your Array according to #Bibberty Comment and do operation
raffles=rafale=[{"author":"Name","rafflename":"RAFFLE","amount":10,"entries":[]}]
raffles[0].entries.push({ 'username': 'example', 'user_id': 1 });
console.log(raffles)

Cannot read property 'teacherId' of undefined when calling the array of object [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
var teacherArray=[];
I have created an array variable.
I am creating an array with the key and value. and pushing these data to the teacherArray.
random={
teacherId:TeacherId,
day:day,
periodCount:period,
class:Studentclass,
section:Studentsection,
startTime:schoolStartTime,
endTime:schoolEndTime
};
teacherArray.push(random);
console.log(teacherArray);
In the console I am able to see the created array. But when the submit button clicked i am calling the array like
teacherLength=teacherArray.length;
for (let k=0;k<=teacherLength;k++)
{
var teachId=teacherArray[k].teacherId;
console.log(teachId);
}
In the console displays the teacherId, but next line shows the error as
TypeError: Cannot read property 'teacherId' of undefined
Arrays are zero index based. So when you write k<=teacherLength you are requesting more than what array have. That should be changed to
k<teacherLength
Arrays are 0 indexed. You are having,
for (let k=0;k<=teacherLength;k++)
Make it,
for (let k=0;k<teacherLength;k++)
Your code is reading an extra item, you need to replace
for (let k=0;k<=teacherLength;k++)
with
for (let k=0;k<teacherLength;k++) //notice < instead of <=

Moving Javascript object from one array to another [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to move an object from one array to another by using the splice and push methods. Here's my code:
console.log(tl.tour.length);
tl.tour.push(shuttleList.splice(ui.draggable.data('keyIndex'), 1)[0]);
console.log(tl.tour.length);
However, if I call that code, the element is getting removed from the shuttleList array, but the tl.tour array stays empty. Both debug log calls print 0.
I should say that shuttleList is a knockout observable, tour is not.
Here is a working example
var a = [1, 3, 5];
var b = [2, 4, 6];
console.log(a, b);
b.push(a.splice(1, 1)[0]);
console.log(a, b);
You could provide the code or try to find what was spliced.
I found the error. I was resetting the tl.tour array in another part of my code.

Need Array inside other Array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
For some reason I need to push an Array inside other array.
For example,
var a = ["Test1", 1];
var b = ["Test2", 2];
var c = [];
c.push(a);
c.push(b);
alert(c);
For this code I need the following output,
["Test1", 1],["Test2", 2]
But what I am getting is
Test1,1,Test2,2
Any help will be highly appreciable.
You've already done it correctly: c.push(a) and c.push(b) work, but you don't want to use alert() for debugging.
Though it might be more convenient since you don't have to open up a console, it's going to give you output that is inconsistent with the actual structure of the data because using alert(x) converts whatever x is to a string.
Always use console.log(). Had you done that in this case, you would have seen something like this in the console:
Demo

How to add property to a global Javascript object inside a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am tying to add a property to a JS object inside a function. I can do it outside but not inside. Please explain. Sorry. I am missing something very basic here.
var newobj = {'prop1' : 12, 'prop2' : 25};
myfunc(newobj);
function myfunc(someobj) {
someobj.prop3 = 45;
}
This gives a syntax error.
Chances are something else is interfering because it works for me.
If you dump newobj before the function call you get:
{"prop1":12,"prop2":25}
And after the function call:
{"prop1":12,"prop2":25,"prop3":45}
As you can see, the new property has been added.
I would suggest either looking at what you have more closesly (make sure you're not copying the value and then passing it) or add some console.log call in your code as it goes through. You can also, in most of the browsers, use the debugger to step through the code to see where it may be fouled.

Categories

Resources