What I am trying to do is if I have Array
a = {1,2,3,4};
b = {1,2};
Then I want subset array as c = {3,4};
Can anybody help me?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m not aware of any built-in way to do this, you basically have to loop through
cand check whether each element is inaand, if so, remove it. TheArray#indexOfmethod can help you with checking, but not all implementations have it (though most do). Removal can be viaArray#splice.So:
…and then either supply your own implementation of
Array#indexOfif your environment doesn’t support it, or use a library like Prototype that supplies it for you (jQuery gives it to you as well, but through its ownjQuery.inArrayfunction). If doing it yourself:Note that adding to the
Arrayprototype as above can be dangerous when done withpoorly-written codecode that makes assumptions about the environment. Specifically, code that treatsfor..inas though it loops through array element indexes (it doesn’t, it looks through object property names) will get messed up if you add to theArrayprototype. (That’s probably why jQuery doesn’t do it.)Live example