Forum Discussion
3 Replies
- baxatobCommunity Hero
The general approach is to go through arrays in cycle and compare every item:
var arr1 = [1,2,3,4] var arr2 = [1,2,3,7] if (arr1.length == arr2.length) for (var i=0; i<arr1.length; i++) if arr1[i] != arr2[i] Log.Error()
However different arrays can have similar items but in different order:
[1,2,3,4] and [4,3,2,1] - for some case they can be defined as equal. And so on...
The right solution depends on your task.
- kruttikaOccasional Contributor
I have a task
where arr1 has all the objects with all the attributes in it , now arr 2 has all the objects from arr1 and + one additional one
Now I have to pick all the objects from arr2[i] and look into arr1 and get the objects and its all attributes from the one which is not in arr1.
Following is the sample code for the same
var arr1 = Page.QuerySelectorAll(.category-container).toArray();
//add new category
var add_category_link = Page.QuerySelector('[data-internal-id="open_category_modal"]').Click();
Page.Wait();
//Select Language
var Sel_Lang=Page.QuerySelector('#lang_selector').ClickItem("English");
//enter Category Name
var Category_Name= Page.QuerySelector('[data-internal-id="category_name_en"]').SetText("dsgfdsgg");
//Click Save
var Add_Product_Save= Page.QuerySelector('#save_category_button').Click();
Page.Wait();
var arr2 = Page.QuerySelectorAll(.category-container).toArray();var arr2_len = arr2.length;
var arr1_len= arr1.length;
//compare two arrays
if (arr1_len == arr2_len)
{
Log.Message("nothing happens");
}
else
{ Log.Message("length not same");
var i=0,j=0,len;
var result;
for (i = 0;i<arr2_len; i++)
{
var category_1= arr2[i];
var attribute_1=category_1.getAttribute(attrib);
var flag_check = false;
j = 0;
for (j=0; j<arr1_len;j++)
{
var category_2= arr1[j];
var attribute_2=category_2.getAttribute(attrib);
if (attribute_1==attribute_2)
{
Log.Message("Match found");
flag_check=true;
break;
}
}
if (flag_check)
{
return category_1;
}
}
return null;
}- tristaanogreEsteemed Contributor
OK... so, what's the error, problem, etc? At first glance, the code looks much like what baxatob posted for doing the comparison... is there a particular problem you're having where your code is not working?