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 7 years ago.
Improve this question
I am trying to push a value into an array and it is giving me this error in the developer tools.
Uncaught TypeError: Cannot read property 'push' of null
Here is the code that it seems to be sticking on, word and local word were defined earlier like this.
var word = [];
var localWord = [];
function setLocalArray() {
// first get words from text field and update word array.
word = document.getElementById("words").value.split(',');
// store word array in localStorage
for(var i=0; word.length > i; i++) {
var key2 = "part"+i;
localStorage.setItem(key2,word[i]);
localWord.push(key2);
}
localStorage.setItem("localWord",JSON.stringify(localWord));
text2Array();
reveal();
}
localWord.push(key2); Seems to be what it is getting stuck on. I have looked at everything I can find on the push method and I can't seem to find why it is giving me this error. Help?
Here is the full code at jsfiddle http://jsfiddle.net/runningman24/jnLtpb6y/
Try this...
var localWord = new Array(); //create new array
var word = new Array();
function setLocalArray() {
word = document.getElementById("words").value.split(',');
// store word array in localStorage
for(var i=0; word.length > i; i++) {
var key2 = "part"+i;
localStorage.setItem(key2,word[i]);
localWord.push(key2);
}
}
I found the problem, if you look in the jsfiddle I posted I am trying to pull localWord from localStorage even though it doesn't exist and so it sets it to null. Thank you to all for the ideas and contributions.
You could try isolating the scope of your variable using the module pattern:
var arrayManager = (function () {
var word = [];
var localWord = [];
function setLocalArray() {
// first get words from text field and update word array.
word = document.getElementById("words").value.split(',');
// store word array in localStorage
for(var i=0; word.length > i; i++) {
var key2 = "part"+i;
localStorage.setItem(key2,word[i]);
localWord.push(key2);
}
localStorage.setItem("localWord",JSON.stringify(localWord));
text2Array();
reveal();
}
return {
setLocalArray:setLocalArray
} ;
}());
and the from the outside you have to simply call arrayManager.setLocalArray()
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Data in Array is Incomplete
it get only last item.
I need to get all item.
how to fix this?
source code
let GetProvinceWithSumCovid19 = [];
for (let index = 0; index < GetProvince.length; index++) {
GetProvinceWithSumCovid19 = dataFromAmCharts.filter(x => x.name.indexOf(GetProvince[index].providersEN) > -1);
GetProvinceWithSumCovid19.push({
value: GetProvince[index].sumCovid19
})
};
console.log(GetProvinceWithSumCovid19);
The first line in the for loop overwrites GetProvinceWithSumCovid19
There's not a lot of information to go off of here, but looking at the second image of the output, I believe you're trying to do something like this:
const getProvinceWithSumCovid19 = GetProvince.map((item, index) => {
return {
[dataFromAmCharts.filter(x => x.name.indexOf(GetProvince[index].providersEN) > -1)],
value: GetProvince[index].sumCovid19
}
});
console.log(getProvinceWithSumCovid19);
Instead of assigning to GetProvinceWithSumCovid19 variable, push to it.
var GetProvinceWithSumCovid19 = [];
for (let index = 0; index < GetProvince.length; index++) {
GetProvinceWithSumCovid19.push(dataFromAmCharts.filter(x => x.name.indexOf(GetProvince[index].providersEN) > -1));
GetProvinceWithSumCovid19.push({
value: GetProvince[index].sumCovid19
})
};
console.log(GetProvinceWithSumCovid19);
The output structure would be like:
[[{id:, name:},{id:, name:}], {value:2}, [{id:, name:}], {value:1}]
But I really doubt whether this is the structure you want. The required output structure is not clear in your question.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a javascript with the unixtimestamp and the price of an item at that particular time. The timestamp is coming in string as listed below. How do I remove the double quotes from the timestamp. This is the array. I used the string replace function but not working.
["1356998400000", 222.69179362385]
["1357084800000", 209.18952317885]
["1357171200000", 211.95012017103]
["1357257600000", 200.15913266219]
["1357344000000", 215.58462758679]
var arr = [["1356998400000", 222.69179362385],
["1357084800000", 209.18952317885],
["1357171200000", 211.95012017103],
["1357257600000", 200.15913266219],
["1357344000000", 215.58462758679]];
arr.forEach(function(item){
item[0] = Number(item[0])
})
console.log(arr);
Just Use Number() to make a string containing number to number.
Something like this?
var myArr = ["1356998400000", 222.69179362385,
"1357084800000", 209.18952317885,
"1357171200000", 211.95012017103,
"1357257600000", 200.15913266219,
"1357344000000", 215.58462758679];
//check the values in the array before making changes
console.log(myArr);
var i;
for(i = 0; i < myArr.length; i++) {
if(typeof myArr[i] == "string") {
myArr[i] = parseFloat(myArr[i]);
}
}
//check the value of the array after changes
console.log(myArr);
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 am trying to get it to return "ha, awesome" followed by the index of the word "awesome"
HTML:
<blockquote id = "blocky">
ha, awesome<br>
</blockquote>
JS:
var x = document.getElementsByTagName(blockquote).innerHTML ;
var n = x.indexOf("awesome");
document.getElementsByTagName(blockquote).innerHTML = x + "<br>" + n;
If I change the JS to this, it works
var x = document.getElementById("blocky").innerHTML ;
var n = x.indexOf("awesome");
document.getElementById("blocky").innerHTML = x + "<br>" + n;
https://jsfiddle.net/mzrt/zaf98g8y/1/
First off, you need to pass it a string, not a variable (unless that variable contains a string).
var x = document.getElementsByTagName('blockquote');
Next, document.getElementsByTagName returns a element collection, not a single element. You can get the first result using [0].
var x = document.getElementsByTagName('blockquote')[0];
Or you can iterate through all of the elements using a for loop.
for (var i = 0; i < x.length; i++) {
var element = x[i];
element.innerHTML = '...';
}
Basically getElementsByTagName will return a node list, an array like object, you cannot access the property of a node from it directly, you have to use bracket notation to fetch the first node from it and then you can treat it as a node object,
var x = document.getElementsByTagName('blockquote')[0].innerHTML;
Since the element that you are targeting is an element with id, It is better to go with getElementById.
var x = document.getElementsByTagName('blockquote')[0].innerHTML ;
var n = x.indexOf("awesome");
document.getElementsByTagName('blockquote')[0].innerHTML = x + "<br>" + n;
Get element by tag name returns a node list, so you have to tell whitch node you do want.
Also you should pass tagname between single quotes.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to iterate over an array and apply an onclick event to each item. I'm hoping to be able to click each of my divs and have them console log their value. Right now I'm stuck on how to apply the onclick to each div. I'm new to JS, so I'm not completely of why I shouldn't make a function inside of a loop like JSBin is complaining about. I've messed around with a lot of different ways to do this, but am truly stuck...
JSBin
function numberTrack() {
var gridItems = document.getElementsByClassName("grid");
for (var i = 0; i < gridItems[0].length; i ++) {
gridItems.onclick = function(){
alert("hello");
};
}
}
numberTrack();
var c = document.getElementsByClassName("divs"); <--- array of divs
for (var i = 0; i < c.length; i++) {
c[i].onclick = function() {
console.log(this.value);
}
}
JSBin is complaining because it wants you to declare the function outside the for loop and then assign it inside the for loop. This is more efficient then what you are currently doing, which is assigning a new anonymous function to each item in the array. All those identical functions will have to be created and stored separately in memory.
You can do something like this instead:
function alertHello() {
alert("hello");
}
for (var i = 0; i < gridItems.length; i++) {
gridItems[i].onclick = alertHello;
}
You need to loop through all the items in the gridItems collection, And inside the loop, get each item using the iterator i value.
function handleClick()
{
alert("hello");
}
function numberTrack() {
var gridItems = document.getElementsByClassName("grid");
for (var i = 0; i < gridItems.length; i ++) {
gridItems[i].onclick = handleClick;
}
}
numberTrack();
If you are allowed to use jQuery, you can bind the event to items like this.
$(function(){
$(document).on("click",".grid",function(e){
var item =$(this);
alert(item.html())
})
})
Here is a working jsBin sample
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Trying to create an array with a loop and then sum all the contents of the array and put the result as the contents of a DIV but when I run the fiddle I get no result and jshint says everything is well formed.
var begin = 500000;
var stop = 999999;
var arrInt = [];
// Create an array worth summing
var CreateArray = function (begin,stop) {
while(begin < stop +1){
arrInt.push(begin++);
}
};
// Sum all ints in an array
var IntSum = function (ManyInts) {
var i = arr.length; while (i--) {
return;
}
};
var example = document.getElementById("example").innerHTML;
example=IntSum(arrInt);
<div id="example"></div>
http://jsfiddle.net/7b8rqme5/
At no point do you call CreateArray. You call your other function, IntSum, which does precisely nothing. Also, you create a variable example and assign a dom element to it, then you immediately overwrite it with a (noop) function result. There are additional issues with your code as well.
My advice: slow down, determine what it is you need to accomplish, and take it step by step.
I think this is what you wanted. But not really sure what you are trying to do here.
var begin = 500000;
var stop = 999999;
var arrInt = [];
var CreateArray = function (begin,stop) {
while(begin < stop +1){
arrInt.push(begin++);
}
};
var IntSum = function (ManyInts) {
var sum = 0
var i = ManyInts.length; while (i--) {
sum += ManyInts[i];
}
return sum;
};
var example = document.getElementById("example").innerHTML;
CreateArray(begin, stop);
var saic=IntSum(arrInt);
document.getElementById("example").innerHTML = saic
http://jsfiddle.net/wpnkL6k2/