Moving Javascript object from one array to another [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 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.

Related

I don't know why I get this result when I use reduce consecutively in 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 months ago.
Improve this question
I found something strange while testing this and that.
function func1(data) {
return data.reduce((prev, x) => {
prev.push(x);
return prev}, []);
}
let arr = [1, 2, 3, 4, 5]
func1(arr).reduce((x) => {
console.log(x)
return x;
})
output:
1
1
1
1
My expectation is that it returns an array, so I wanted to print the values ​​for [1, 2, 3, 4, 5] in the next reduce, but it failed. Why did you get this result?
Reduce operation reduces inputs with the operation supplied in your function starting with an initial value (or the first element if no initial value supplied) Each iteration takes the previous response and next array element to evaluate the same function.
Since your function does not have an initial element it takes your first element "1" to initialize. And your function does not evaluate anything but returns the element you first supplied. Therefore you always print your first element as the result.
You can check these examples to better understand reduce operation.

Why does this Javascript code return an empty 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 2 years ago.
Improve this question
I got this exercise from freeCodeCamp. When I try to solve it in the following way, it returns an empty array. Why?
function frankenSplice(arr1, arr2, n) {
return arr2.splice(n, 0, arr1);
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
The .splice() method returns an array containing deleted elements. Your code is requesting that 0 elements be deleted, so the returned array will of course be empty.

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 <=

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

Defining String Arrarys [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
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.

Categories

Resources