Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-29-2012, 02:57 PM   PM User | #1
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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
triko is offline   Reply With Quote
Old 12-29-2012, 04:17 PM   PM User | #2
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
See JavaScript Array Object on the site w3scholls.com and particularly the methods(*) concat(), slice() or splice()...

(*) See too the examples of each method.

Last edited by 007julien; 12-29-2012 at 04:19 PM..
007julien is offline   Reply With Quote
Old 12-29-2012, 04:19 PM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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.

Last edited by jmrker; 12-29-2012 at 04:21 PM.. Reason: Hunt and peck on tablet.
jmrker is offline   Reply With Quote
Old 12-29-2012, 05:18 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 12-29-2012, 07:59 PM   PM User | #5
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Thanks so much at all!!!
Philip Supreme Master
triko is offline   Reply With Quote
Old 12-29-2012, 08:01 PM   PM User | #6
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
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???
triko is offline   Reply With Quote
Old 12-29-2012, 09:00 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by triko View Post
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
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 12-30-2012, 03:13 AM   PM User | #8
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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);
}
triko is offline   Reply With Quote
Old 12-30-2012, 04:21 AM   PM User | #9
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Question

Quote:
Originally Posted by triko View Post
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?
jmrker is offline   Reply With Quote
Old 12-30-2012, 04:58 AM   PM User | #10
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,448
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by triko View Post
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);
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is online now   Reply With Quote
Old 12-30-2012, 01:00 PM   PM User | #11
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by jmrker View Post
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!!!!
triko is offline   Reply With Quote
Old 12-30-2012, 01:01 PM   PM User | #12
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
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!!!
triko is offline   Reply With Quote
Old 12-30-2012, 01:10 PM   PM User | #13
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by triko View Post
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, " "));
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 12-31-2012, 03:23 AM   PM User | #14
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

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>
jmrker is offline   Reply With Quote
Old 12-31-2012, 04:30 AM   PM User | #15
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by triko View Post
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>

Last edited by Logic Ali; 12-31-2012 at 04:45 AM..
Logic Ali is offline   Reply With Quote
Users who have thanked Logic Ali for this post:
jmrker (12-31-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:29 PM.


Advertisement
Log in to turn off these ads.