Note that there are some explanatory texts on larger screens.

plurals
  1. POSuggest elements of an object array which is not there in another object array
    primarykey
    data
    text
    <p>I have two array of objects.</p> <pre><code>Array1 : [{"id":20,"stName":"ABC","className":"A"},{"id":30,"stName":"ABD","className":"B"},{"id":40,"stName":"ABE","className":"C"},{"id":50,"stName":"ABF","className":"D"}] Array2 : [{"id":110,"stName":"ASA","className":"X"},{"id":120,"stName":"ASB","className":"Y"},{"id":130,"stName":"ASC","className":"A"},{"id":140,"stName":"ASD","className":"C"},{"id":150,"stName":"ASE","className":"Z"}] </code></pre> <p>Here </p> <blockquote> <p>array1 have classNames as A, B, C, and D.</p> <p>array2 have classNames as X, Y, A, C, and Z</p> </blockquote> <p>A function should return the <code>classNames</code> of <code>array2</code> such a way that the <code>classNames</code> does not belong to <code>array1</code></p> <blockquote> <p>The return from the function will be an array which contains X, Y, and Z as elements.</p> </blockquote> <p>How to write this function in <code>javascript</code> with less time complexity, because array1 and array2 may have more than 20 objects.</p> <p><strong>Edit</strong> </p> <p>This is the script which I used using <code>for loop</code></p> <pre><code>array1 = [{"id":20,"stName":"ABC","className":"A"},{"id":30,"stName":"ABD","className":"B"},{"id":40,"stName":"ABE","className":"C"},{"id":50,"stName":"ABF","className":"D"}] array2 = [{"id":110,"stName":"ASA","className":"X"},{"id":120,"stName":"ASB","className":"Y"},{"id":130,"stName":"ASC","className":"A"},{"id":140,"stName":"ASD","className":"C"},{"id":150,"stName":"ASE","className":"Z"}] function findSuggest(){ var sug = []; for(array2_count=0;array2_count &lt; array2.length;array2_count++){ for(array1_count=0;array1_count &lt; array1.length;array1_count++){ if(array2[array2_count].className == array1[array1_count].className){ break; } else{ if(array1_count == (array1.length - 1)){ sug[sug.length] = array2[array2_count].className; } } } } } </code></pre> <p>Here <code>sug[]</code> will have all suggestions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload