web_development:js:js-arrays

Javascript Arrays

nesting arrays into arrays/objects

var postIds = [ 376, 400, 764 ];
    userIds = [ 1, 4, 9 ];
    data = [ postIds, userIds ];

console.log( data );

data = {
    postIds: postIds,
    userIds: userIds
};

console.log( data );

How to use push and pop and splice

//Remove from end of array
postIds.pop();
console.log( postIds);

// Remove from beginning of array
// postIds.shift();
// console.log( postIds );

//Remove based on index
// Takes index, number items to be removed
// postIds.splice( 2, 1);
// console.log( postIds );

More Array Options:

* get number of items in an array -

console.log( postIds.length );

* get last item in an Array -

console.log( postIds[ postIds.length - 1 ] );

* join arrays together -

var savedPostIds = [ 3, 7, 18, 25, 38 ],
    newPostIds = [ 77, 84, 93, 101 ],
    postIds;

* select portion of arrays using slice -

var postIds = [ 3, 7, 18, 25, 38, 44, 57, 64, 72 ],
    selectedPosts;
// starting with index of 2, and ending with index of 7. (start with 3rd item, before getting to 8th)
selectedPostIds = postIds.slice( 2, 7 );
console.log( selectedPostIds );
/********************************
 * An Array of Objects
 * 1.2.9.4
 *
 *******************************/

// var posts = [
//   {
//     id: 1,
//     title: 'Hello World'
//   },
//   {
//     id: 2,
//     title: 'Hello JavaScript'
//   },  
//   {
//     id: 3,
//     title: 'Hello Arrays!'
//   }
// ];

// console.log( posts );





/********************************
 * Nested Arrays
 * 1.2.9.5
 *
 *******************************/

// var postIds = [ 376, 400, 764 ];
//     userIds = [ 1, 4, 9 ];
//     data = [ postIds, userIds ];

// console.log( data );






/********************************
 * Nested Arrays verses Objects
 * 1.2.9.6
 *
 *******************************/

// var postIds = [ 376, 400, 764 ];
//     userIds = [ 1, 4, 9 ];
//     data = [ postIds, userIds ];

// console.log( data );

// data = {
//   postIds: postIds,
//   userIds: userIds
// };

// console.log( data );




/********************************
 * Getting a value based Index
 * 1.2.9.7
 *
 *******************************/

// var postIds = [ 3, 7, 18, 25 ];

// console.log( postIds[ 0 ] );
// console.log( postIds[ 1 ] );
// console.log( postIds[ 4 ] );



/********************************
 * Reassigning a value in an
 * Array
 * 1.2.9.8
 *
 *******************************/

// var postIds = [ 3, 7, 18, 25 ];

// postIds[ 0 ] = 5;

// console.log( postIds );





/********************************
 * Adding a value to an Array
 * 1.2.9.9
 *
 *******************************/

// var postIds = [ 3, 7, 18, 25 ];

// postIds.push( 32 );
// console.log( postIds );

// postIds.push( 64, 78 );
// console.log( postIds );

// postIds.unshift( 32 );
// console.log( postIds );




/********************************
 * Removing a value from an Array
 * 1.2.9.10
 *
 *******************************/

// var postIds = [ 3, 7, 18, 25 ];

// Remove from end of array
// postIds.pop();
// console.log( postIds );

// Remove from beginning of array
// postIds.shift();
// console.log( postIds );

// Remove based on index
// Takes index, number items to be removed
// postIds.splice( 2, 1 );
// console.log( postIds );




/********************************
 * Getting length of an Array
 * 1.2.9.11
 *
 *******************************/

// var postIds = [ 3, 7, 18, 25, 38 ];

// // Get the number of items in an Array
// console.log( postIds.length );

// // // Get the last item in an Array
// console.log( postIds[ postIds.length ] );
// console.log( postIds[ postIds.length - 1] );



/********************************
 * Concatenating Arrays
 * 1.2.9.12
 *
 *******************************/

// var savedPostIds = [ 3, 7, 18, 25, 38 ],
//     newPostIds = [ 77, 84, 93, 101 ],
//     postIds;

// postIds = savedPostIds.concat( newPostIds );
// console.log( postIds );





/********************************
 * Slicing Arrays
 * 1.2.9.13
 *
 *******************************/

// var postIds = [ 3, 7, 18, 25, 38, 44, 57, 64, 72 ],
//     selectedPosts;

// selectedPostIds = postIds.slice( 2, 7 );
// console.log( selectedPostIds );





/********************************
 * Sorting Arrays
 * 1.2.9.14
 *
 *******************************/

var postTitles = [
      'JavaScript',
      'WordPress',
      'Arrays'
    ],
    postIds = [ 7, 44, 13, 21, 71 ];

// // Sort Text Ascending
// postTitles.sort();
// console.log( postTitles );

// // Sort Text Descending
// postTitles.reverse();
// console.log( postTitles );


// // Does not Sort as we would expect
// postIds.sort();
// console.log( postIds );

// // Sort Numbers Ascending
// postIds.sort( function( a, b ) {return a - b;} );
// console.log( postIds );

// // Sort Numbers Descending - Incorrect
// postIds.sort( function( a, b ) {return a + b;} );
// console.log( postIds );


// // FIX: Sort Numbers Descending - Correct
// postIds.sort( function( a, b ) {return b - a;} );
// console.log( postIds );






/********************************
 * .forEach
 * 1.2.9.15
 *
 *******************************/

// var postTitles = [
//       'JavaScript',
//       'WordPress',
//       'Arrays'
//     ];

// postTitles.forEach( function( title ) {
 
//   console.log( 'Title: ' + title );
  
// });






/********************************
 * .isArray
 * 1.2.9.16
 *
 *******************************/

// var postIds =  [ 7, 44, 13, 21 ],
//     postId = 7;

// console.log( Array.isArray( postIds ) );
// console.log( Array.isArray( postId ) );









/********************************
 * Convert Arrays to Strings
 * and Strings to Arays
 * 1.2.9.17
 *
 *******************************/

// var postTitles = [
//       'JavaScript',
//       'WordPress',
//       'Arrays'
//     ],
//     userFields = 'Name, Email, Website',
//     url = 'http://domain.com/learn/javascript/#content';


// // Convert an Array to a String
// console.log( postTitles.toString() );

// // Convert a String to an Array
// console.log( userFields.split( ',' ) );

// // Convert a String to an Array
// console.log( url.split( '/' ) );
  • web_development/js/js-arrays.txt
  • Last modified: 2020/09/18 01:48
  • by jimboobrien