0

how do i add each of the contents of array_A, into the last empty position of array_B. So basically, i'm trying to fill the array from the top of the stack, down, using the contents of another array...if that makes sense.

function PopulateArrayDown() {

//Declare an empty array and its size
var myArray = [];
myArray.length = 10;

//Get last index 
var lastElement =myArray.length;

//Create array of data
var Data = [1, 2, 3, 4, 5];

//For each value in data, add it to last position in myArray
$.each(Data, function (lastElement, value) {
    myArray.push(lastElement, value);
    lastElement -1;

});

So after this i hoped to see at [10] : 1, at [9] : 2, at [8] : 3 ect.. Fail! lastElement seemed to get reset to 0 as soon as the code got into the .each()

2
  • check for a null or empty value in the array. If it is empty or null insert your value from array B. Commented Oct 2, 2014 at 19:29
  • Would i not need to know the index of that empty element first? to ensure im inserting the new value at the top most element in the array Commented Oct 2, 2014 at 20:27

4 Answers 4

1

If you want a reverse version of an array, here a simple code:

var Data = [1, 2, 3, 4, 5];
var myArray = Data.slice(0, Data.length).reverse(); // make a copy and reverse
myArray; // [5, 4, 3, 2, 1];

If you want your code to work:

//Declare an empty array and its size
var myArray = [];
myArray.length = 10;

//Get last index 
var lastElement = myArray.length - 1;

//Create array of data
var Data = [1, 2, 3, 4, 5];

//For each value in data, add it to last position in myArray
$.each(Data, function (id, value) {
    myArray[lastElement] = value;
    lastElement--;
});
myArray; // [undefined × 5, 5, 4, 3, 2, 1]
  • if you array length is 10, the last index is 9 (length - 1)
  • $.each function first argument is an index, don't put the same name of the variable you need to access
  • lastElement - 1 does nothing, but lastElement-- is like lastElement-=1 like lastElement = lastElement - 1
Sign up to request clarification or add additional context in comments.

2 Comments

Aah, now i see. Thanks for explaining Nick :)
Good, you can validate the answer now :-)
0

You don't need to use jQuery for such a simple problem. I would recommend something like this:

var myArray = [];
myArray.length = 10;
var Data = [1, 2, 3, 4, 5];
var lastElement =myArray.length;
for (var i=0; i< Data.length; i++){
  myArray[lastElement-i] = Data[i];
}

This assumes Data and Array have the same number of elements, which sounds like what you wanted.

2 Comments

for..in is intended to be used on object properties, not arrays.
That for loop on first glance looks pretty intense, but kinda makes sense. Think i just need to try an read through it to understand it. Also, I'm going to try an add another (3rd) array values into the remaining empty elements using this and push.
0

var a=[1,2,3,4,5,6];
var b =[9,56,46,34,78,97];var j =1;
for(i=a.length-1;i>-1;i--){
b[b.length+j]=a[i];
j++;
}
console.log(b);

[9, 56, 46, 34, 78, 97, 7: 6, 10: 5, 14: 4, 19: 3, 25: 2, 32: 1]

Comments

0

Javascript arrays can be made to work like lists, or stacks, or queues, whatever you need. Use push to append and unshift to prepend items. So your code could look like

var myArray = [],
    Data = [1, 2, 3, 4, 5];

for(var i=0; i<Data.length; i++) {
    myArray.unshift(Data[i]);
}

1 Comment

Great tip thanks LL, im really getting into javascript/jquery so this is great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.