Google script change a value in my array unexpectedly [duplicate] - javascript

This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
Closed 5 years ago.
I am stuck with a part of my google script code where one of array elements changed unexpectedly. It looks like a bug to me, but I'd like to hear from here if it's really is or just my coding error that I overlooked. Of course, I'm hoping for a solution as well in any case.
Here is that part of the code.
if (chkIn) {arr[1] = importData[i][1]+'2';
} else {
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
arr[1] = importData[i][1]+'1';
Logger.log('---------------------------------------------------');
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
};
(The if statement doesn't seem relevant here, but I included it just in case.)
Here is the output.
2573 2017122103
2574 20171221041
2575 20171221042
20171221042
---------------------------------------------------
2573 2017122103
2574 20171221041
2575 20171221031
20171221031
I really have no idea how importData[i+2][1] changed its value to arr[1] (the number after 2575).
Thank you in advance.

Probably this is because in your case:
arr === importData[i+2]
So when you change arr[1] you also have changed importData[i+2][1].

Related

how to append to list below example given and output also? [duplicate]

This question already has an answer here:
How to merge each object within arrays by index?
(1 answer)
Closed 4 months ago.
This is the data:
data=[{"id":1,"name":"vikash","roll":39},{"id":2,"name":"kumar","roll":3}]
data2=[{"hobby":"football","food":"any"},{"hobby":"basketball","food":"any"}]
list1=[]
expected Output:
list1:[{"name":"vikash","roll":39,"hobby":"football","food":"any"},{"name":"kumar","roll":3,"hobby":"basketball","food":"any"}]
Please help me!
Providing both lists are the same length, you're happy there are no key conflicts and you're using Python...
data=[{"id":1,"name":"vikash","roll":39},{"id":2,"name":"kumar","roll":3}]
data2=[{"hobby":"football","food":"any"},{"hobby":"basketball","food":"any"}]
list1 = []
for k, v in enumerate(data):
list1.append({**v, **data2[k]})
print(list1)
However in the future please be more concise when asking your questions.
Specify the language
Don't include redundant tags (eg - this question has nothing to do with Django)
Tell us what you've tried!
Also... the "please help me" line isn't required. The whole purpose of this community is to help each other so again this is redundant!
A simpler to understand solution
data=[{"id":1,"name":"vikash","roll":39},{"id":2,"name":"kumar","roll":3}]
data2=[{"hobby":"football","food":"any"},{"hobby":"basketball","food":"any"}]
list1=[]
for i in range(len(data)):
del data[i]['id']
d_main = {}
d_main.update(data[i])
d_main.update(data2[i])
list1.append(d_main)
print(list1)

Calling a nested function with a string (window[]) in JS [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 7 months ago.
How to call a nested function with only a string? eg:
function oot(wha) {
function inn(wha)
{
First.innerHTML+="Inner ["+wha+"]";
}
oot.inn = inn;
Second.innerHTML+="Outer ["+wha+"]";
}
oot("1");
oot.inn("2"); //works okay
window["oot"]("3"); //works okay
window["oot.inn"]("4"); //<The problem, doesn't work.
window["oot"]["inn"]("4"); //Works, thanks.
Edited to make the code more readable, and show a solution.
IF there is no way to make this work with a single string i can probably make do, but i will leave the question unanswered for a few hours to see if there is another solution.
You can reference nested Objects like this:
window["oot"]["inn"]("4");
or
window.oot.inn("4")

why do you have to add the [I]? [duplicate]

This question already has answers here:
How to get value at a specific index of array In JavaScript?
(8 answers)
Closed 2 years ago.
so I am currently learning javascript on Codecademy. And a weird kind of thing got introduced that I don't quite get.
if you look at the code. you see at the end in console that after logging animals it is a [i] like why is that there? I get that is has something to do with the for loop. But I don't quite understand like why or what that it does. I don't know if question is clear enough but if you just try to explain why it is there and what it does there. that would be greatly appreciated:)
const animals = ['Grizzly Bear', 'Sloth', 'Sea Lion'];
for (let i = 0; i < animals.length; i++){
console.log(animals[i]);
}
the bracket notation is used to specify the index of the array you are looping through. You can always read more about javascript arrays on w3schools

Please tell me what this code means [duplicate]

This question already has answers here:
What is the result of this javascript?
(3 answers)
Closed 5 years ago.
can you tell me what this code does as I need to understand it for a coding assessment. I only just started learning JavaScript
function getAttackString() {
var foo = "d323b8b34";
var bar = "x334q3j98";
return "The code is: "+(foo.substr(3,foo.length-6))+(bar.substr(2));
}
Thanks for your help
Start by looking up what .length and .substr are.
For example, what will foo.length give you? A good way to find out would be:
var x = "abc123";
console.log(x.length);
console.log is a command to print information to your web browser console, very helpful when doing debugging work.

jQuery parseInt() not working [closed]

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 6 years ago.
Improve this question
I have this code to add elements to my table and that part works perfectly fine. However, I want to add up the values of the last column in order to later print that on another part of the page. parseInt doesn't work for some reason (the values don't come back as integers and stay as strings, is there anything I have done wrong? Again, adding to the table works perfectly fine.
var hourC = 0;
var hourC =+ parseInt($(".hourCount").text(), 10);
alert(hourC);
Edit:
When I print the values of the variable hourC they don't add up to the previous value, they just stay next to each other. Example: 1 + 1 = 11 rather than 2. I don't see where my issue is and the answer for debugging didn't help since I still got the same result.
Final Edit:
I achieved what I wanted now through a different medium, I created an array and pushed the values into the array and then I used "join" to solve the issue.
If interested in what I was asking for here is a fiddle with the final result. (You can just change the console.log to alert)
Basic debugging skills.
parseInt works -- its a well tested function. If it broke in a browser, a lot of people would notice.
However, you haven't given any way to figure out what is going on with your code, since the real problem MUST be here:
$(".hourCount").text()
That must not contain whatever value you think it does.
Split your code up and use the debugger, or even console log, to see what the values are.
var hourC = 0;
var strValue = $(".hourCount").text();
alert(["strValue = ", strValue]);
hourC = parseInt(strValue, 10);
alert(hourC);

Categories

Resources