javascript - remove array element if it doesn't exist -


i want remove element array ids if element doesn't exist .

<div id="nl-form-0" >    <input type="text" id='dynamic_translation_0_0' value="15" />   <input type="text" id='dynamic_translation_0_1' value="15" />   <input type="text" id='dynamic_translation_0_2' value="15" />   <input type="text" id='dynamic_translation_1_2' value="15" /> </div>   ids = [   "transliteratetextarea",   "dynamic_translation_0_0",   "dynamic_translation_0_1",   "dynamic_translation_0_2",   "dynamic_translation_1_0",   "dynamic_translation_1_1",   "dynamic_translation_1_2"];  check_remove_ids_array(ids); console.log(ids); console.log($("#dynamic_translation_1_1").length);  function check_remove_ids_array(array_in) {   array_length = array_in.length;    (n = 0; n <= array_length; n++) {      if ($("#" + array_in[n]).length == '0') {         removevalue(ids,array_in[n]);     }             }      }  function removevalue(arr, value) {   var array = arr;    (var = array.length-1; i--;) {     if (array[i] === value) {       array.splice(i, 1);     }   }    return array; } 

in above code dynamic_translation_1_1 not exist. want remove or other element array ids if element doesn't exist

why does/did not work?

basically had return , assigning problem. since there no reference parameters in javascript have assign returned array initial ids. same goes removevalue() function.

suggestion

use array.filter() prototype instead. solves problem more elegantly.

example

<html>     <head>         <script src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js'></script>          <script>             $(document).ready(function(){                 ids = ["transliteratetextarea", "dynamic_translation_0_0", "dynamic_translation_0_1", "dynamic_translation_0_2", "dynamic_translation_1_0", "dynamic_translation_1_1", "dynamic_translation_1_2"];                 ids = check_remove_ids_array(ids); //we have assign back, else initial ids array!                 console.log(ids);                 console.log($("#dynamic_translation_1_1").length);             });              function check_remove_ids_array(array_in){                 array_length = array_in.length; //is not required in example anymore.                  //we use filter function instead.                 return array_in.filter(function(item){return $("#" + item).length > 0})             };         </script>     </head>      <body>         <input type = 'text' id = 'dynamic_translation_0_0' value = '15' />         <input type = 'text' id = 'dynamic_translation_0_1' value = '15' />         <input type = 'text' id = 'dynamic_translation_0_2' value = '15' />         <input type = 'text' id = 'dynamic_translation_1_2' value = '15' />     </body> </html> 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -