/    Sign up×
Community /Pin to ProfileBookmark

check array contains another array element

Hello Experts,

I’m trying to looping through array(a) and wanted to check if current element exists in another array (b). if current element is not array (b), then add element array(b). but I’m not getting correct results.

[code=html]
fArray1 = this is query and getting following results ([cn=user1,ou=orga,ou=orgb,o=org, cn=user2,ou=orga,ou=orgb,o=org, cn=user3,ou=orga,ou=orgb,o=org,]}
for (i = 0; i < fArray1.size(); i++)
{
if (fArray1)
{
var chkarray1 = finalArray.toString(); /*finalarray has ([cn=user2,ou=orga,ou=orgb,o=org, cn=user3,ou=orga,ou=orgb,o=org, cn=user4,ou=orga,ou=orgb,o=org, cn=user5,ou=orga,ou=orgb,o=org]]} */
var testarry1 = fArray1.get(i).toString();

if (fArray1.get(i).toString()=== ‘chkarray1’) /* tried this but did not work */
// if (chkarray1.indexOf(fArray1.get[i]) != -1) /* tried this but did not work */
{
trace out
}
else
{
finalArray.add(testarry1.get(i));
}
}
}
[/code]

finalarray should have [cn=user1,ou=orga,ou=orgb,o=org, cn=user2,ou=orga,ou=orgb,o=org, cn=user3,ou=orga,ou=orgb,o=org, cn=user4,ou=orga,ou=orgb,o=org, cn=user5,ou=orga,ou=orgb,o=org].

please advise.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@Sup3rkirbyFeb 11.2015 — As much as I'd like to help, you've got a few problems with the information you posted. Obviously the code can't be run as-is. It'd be helpful to get the code you are actually running, without the extra comments and text that break the script (as well as additional code that seems to be missing such as variable declarations).

Also, since the point of this is to check two different arrays and append values if they do not exist, it would be helpful to provide the two arrays in question. The arrays in your code are malformed (formatted incorrectly) and so I can't really rely on those. I could write you a simple script that compares to arrays and adds values to the second array if they do not exist, but based on the way you seem to be formatting your array values it would seem you may need something more complex.


[B][EDIT][/B]

Actually, the more I look at it, I have no idea what your code is even supposed to be. The [B]size()[/B] method isn't a valid javascript method and would only exist if you are using some sort of plugin/library. The same goes for [B]add()[/B] (with the exception of [I]select objects[/I] or the new and experimental [B]Set()[/B] objects). I would have to guess the same goes for the [B]get()[/B] method you are using.

Or in other words, it doesn't look like any of the code you posted is usable. So if I ignore all of that and just consider your basic question, looping through array (a) and checking to see if the value exist in array (b), adding it if it doesn't exist, then your solution is simple (assuming you really are talking about standard arrays).
[CODE]
var $a = [1, 2, 3, 4, 5, 6, 7, 8, 9],
$b = [0, 1, 3, 5, 7, 9, 11, 13];

for(var $i = 0; $i < $a.length; $i++) {
if($b.indexOf($a[$i]) < 0) $b.push($a[$i]);
}
[/CODE]

This takes two arrays ([B]$a[/B] and [B]$b[/B]) and compares them. Anything in array [B]$a[/B] that is missing from array [B]$b[/B] will be added.
×

Success!

Help @srive99 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...