admin管理员组

文章数量:1391969

I have two array of objects.

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"}]

Here

array1 have classNames as A, B, C, and D.

array2 have classNames as X, Y, A, C, and Z

A function should return the classNames of array2 such a way that the classNames does not belong to array1

The return from the function will be an array which contains X, Y, and Z as elements.

How to write this function in javascript with less time plexity, because array1 and array2 may have more than 20 objects.

Edit

This is the script which I used using for loop

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 < array2.length;array2_count++){
        for(array1_count=0;array1_count < 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;

                }
            }   
        }
    }


}

Here sug[] will have all suggestions.

I have two array of objects.

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"}]

Here

array1 have classNames as A, B, C, and D.

array2 have classNames as X, Y, A, C, and Z

A function should return the classNames of array2 such a way that the classNames does not belong to array1

The return from the function will be an array which contains X, Y, and Z as elements.

How to write this function in javascript with less time plexity, because array1 and array2 may have more than 20 objects.

Edit

This is the script which I used using for loop

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 < array2.length;array2_count++){
        for(array1_count=0;array1_count < 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;

                }
            }   
        }
    }


}

Here sug[] will have all suggestions.

Share Improve this question edited Jul 3, 2013 at 5:07 Javad Shareef asked Jul 2, 2013 at 14:34 Javad ShareefJavad Shareef 4968 silver badges19 bronze badges 4
  • 1 Do you have anything beyond the task description? This sounds like a homework assignment and you want someone to do it for you. – Tomalak Commented Jul 2, 2013 at 14:38
  • This is a part of a product am working on, and the data which I provided is just for simplify the question. This snippet a part of an algorithm, which need more efficiency. I already implemented this with normal for loop, but want to improve the efficiency of it. – Javad Shareef Commented Jul 2, 2013 at 14:42
  • @lucuma I tried this by iterating to every element and pare its values. It is more time consuming if more number of objects are there in an array. – Javad Shareef Commented Jul 2, 2013 at 14:44
  • 1 @JavadShareef Update your question with the code you've tried that you need help with. – lucuma Commented Jul 2, 2013 at 14:46
Add a ment  | 

1 Answer 1

Reset to default 18

Here's the general idea of the algorithm:

  • Iterate over Array1.
    • Add the current item's className to a truth map
  • Iterate over Array2
    • If the current item's className is not in the truth map, add it to the result.

That simple, O(n+m) (worst case O(2n)). By a truth map, my intention is a plain js object, where each key is (in this case) a className, and each value is true.

本文标签: javascriptSuggest elements of an object array which is not there in another object arrayStack Overflow