Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Help Array!!!!!

1. Help Array!!!!!

Hello guy! I have a problem with array, i don't know how to build a new array using the numbers of other array!
EX: I have
array1 = [1, 4, 2, 13, 5]
with this array1 I want to build other array2 using the numbers in the first array. So i don't know how to take number from array and put it in an other array!
Thanks all

• See JavaScript Array Object on the site w3scholls.com and particularly the methods(*) concat(), slice() or splice()...

(*) See too the examples of each method.

• Several methods,

.push()
.slice()
.splice()
.concat()

And there may be more I have not thought of just off the cuff.

Dang, I type too slow.

• Code:
```var array1 = [1, 4, 2, 13, 5];
var array2 = [];
for var (i=0; i<array1.length; i++) {
array2[i] = array1[i];
}```
When you are playing all day, every year, it makes a difference. - Football team coach

• Thanks so much at all!!!
Philip Supreme Master

• Originally Posted by Philip M
Code:
```var array1 = [1, 4, 2, 13, 5];
var array2 = [];
for var (i=0; i<array1.length; i++) {
array2[i] = array1[i];
}```
When you are playing all day, every year, it makes a difference. - Football team coach
But if I want put in array2 only the even or odd number???
How to do???

• Originally Posted by triko
But if I want put in array2 only the even or odd number???
How to do???
What does that mean? Do you mean only the odd/even indices (1,3,5 etc. or 0,2,4 etc.) or do you mean populate the second array only with numbers in the first array which are odd/even? (1,13,5 are odd in your example).

Surely you can guess that this would involve an if...else statement.

if (array1[i]%2 == 0) // value is even

• Yes using your first method, im solve alone but now i have an other problem
All code is write right but, remainder is NaN and evenOdd don't have value of "i"
Code:
```function exchangeSymmetric ()
{
var arrayPrincipal = [4, 2, 13, 0, 5];
var array1 = [];
var array2 = [];
for (i = 0; i < arrayPrincipal.length; i++)
{
var evenOdd = array1 [i];
var remainder = evenOdd%2;
}
alert (remainder);
}```

• Originally Posted by triko
Yes using your first method, im solve alone but now i have an other problem
All code is write right but, remainder is NaN and evenOdd don't have value of "i"
Code:
```function exchangeSymmetric ()
{
var arrayPrincipal = [4, 2, 13, 0, 5];
var array1 = [];
var array2 = [];
for (i = 0; i < arrayPrincipal.length; i++)
{
var evenOdd = array1 [i];
var remainder = evenOdd%2;
}
alert (remainder);
}```
Your code makes no sense to me.
You create an arrayPrinciple and use the length in a for...loop,
but you never check its contents.

When you make an assignment to the evenOdd variable,
you are using an element position in an empty array1 ... hence the NaN error.

You save the last remainder of the tests which is what becomes alerted,
but it is outside the for...loop which is why only one alert occurs.

What is it that you are really trying to accomplish?

• Originally Posted by triko
Hello guy! I have a problem with array, i don't know how to build a new array using the numbers of other array!
EX: I have
array1 = [1, 4, 2, 13, 5]
with this array1 I want to build other array2 using the numbers in the first array. So i don't know how to take number from array and put it in an other array!
Thanks all
A simple way to make a copy of an array without needing a loop.

Code:
```array1 =  [1, 4, 2, 13, 5];
array2 = [].concat(array1);```

• Originally Posted by jmrker
When you make an assignment to the evenOdd variable,
you are using an element position in an empty array1 ... hence the NaN error.

What is it that you are really trying to accomplish?
Yes You have reason... no sense, i'm wake up this morning and my brain asked me the answer This is the final problem
Code:
```function exchangeSymmetric ()
{
var arrayPrincipal = [4, 2, 13, 0, 5];
var arrayEven = [];
var arrayOdd = [];
for (i = 0; i < arrayPrincipal.length; i++)
{
var evenOdd = arrayPrincipal [i];
var remainder = evenOdd%2;
if (remainder == 0)
arrayEven [i] = arrayPrincipal [i];
else
arrayOdd [i] = arrayPrincipal [i];
}
alert ("This is Array Even" + " " + arrayEven);
alert ("This is Array Odd" + " " + arrayOdd);
}```
This is work!
Only one problem, between the number that is the " comma " " , " !!! Don t know how to remove!!!!

• Originally Posted by felgall
A simple way to make a copy of an array without needing a loop.

Code:
```array1 =  [1, 4, 2, 13, 5];
array2 = [].concat(array1);```
Yes fegal, I have test, but don't is this my problem, I have explained bad!!!

• Originally Posted by triko
Only one problem, between the number that is the " comma " " , " !!! Don t know how to remove!!!!
Code:
`alert("This is ArrayEven " + arrayEven.join(" ").replace(/\s+/g, " "));`

• The default display of an array contents is with ',' separators.
You can alter this display with a .join() as in the following example.
Code:
```<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
<title> Untitled </title>
<script type="text/javascript">
var arrayPrincipal = [4, 2, 13, 0, 5];
var str = '';
str += arrayPrincipal+'\n\n'+arrayPrincipal.join(',')+'\n\n';
str += arrayPrincipal.join(' ')+'\n\n'+arrayPrincipal.join(', ');
alert(str);
</script>
</head>
<body>

</body>
</html>```

• Originally Posted by triko
But if I want put in array2 only the even or odd number???
How to do???
The .slice method generates a new array from an existing one.
For recent browsers that support the .filter method, you can just do:

Code:
```<script type="text/javascript">

array1 = [ 1, 4, 2, 13, 5 ];

odds = array1.filter( function( elem ){ return elem % 2; } )

alert( odds );

</script>```

• Users who have thanked Logic Ali for this post:

jmrker (12-31-2012)

•

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•