Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 01-23-2007, 10:36 AM array splice
numbenator's Avatar
Webmaster Talker

Posts: 516
Location: London
Trades: 0
Hi

i have a element in a array form_links called "motor"

in brief,

I have the below code.

linkN = "motor"

form_links.splice(linkN,1,"removed");

This doeswork though... it's adding a new element at a new location in array

Why is this. I simply wish to either completely remove the element or replace with "removed"

cheers
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
 
Register now for full access!
Old 01-23-2007, 03:33 PM Re: array splice
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Well. it seems logical to mee that this adds a new element, given the reference I've got:
http://http://docs.sun.com/source/816-6408-10/array.htm#1193766
Quote:
Changes the content of an array, adding new elements while removing old elements


If you want to remove an element, you cannot that easily.
You need to parse your array, and recopy a new one, excluding the parts you don't want.

But, you can more easily replace the value of your array with removed.
Use this (javascript function.I do hope the formatting stay....):
PHP Code:
/**
 *Return the specified array with every occurence who have _lookup as value replaced by _replace
 * @param     Array          _array               The array to parse
 * @param     String         _lookup             The string to look up for
 * @param     String         _replace            The string to replace _lookup with
 * @return      Array        
 */
function replaceVal(_array,_lookup,_replace){
  for(var 
i=0;i<_array.length;i++){
        if(
_array[i]==_lookup){
            
_array[i]=_replace;
        }
  }
    return 
_array;

Then calling replaceVal(
form_links,linkN,'removed');
will replace every occurences of linkN in the array with the text removed.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to array splice
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.18856 seconds with 12 queries