1

What is the shorthand and best way to find intersection?

f = ["A","B","C","D","E","F"]; //might be less than 8
b = [1,0,0,1,0,0,0,0];   //always 8 elements 

Desired resulting array ["A","D"]

3
  • 1
    why "C", at it's the third element in f? shouldn't be D ? Commented Apr 4, 2017 at 11:17
  • 1
    Either your desired is wrong or i did not get it.. Should be ["A","D"] Commented Apr 4, 2017 at 11:17
  • Yes, you are right. I corrected the mistake. thanks for notification Commented Apr 4, 2017 at 11:23

5 Answers 5

4

You could use Array#filter

var f = ["A", "B", "C", "D", "E", "F"],
    b = [1, 0, 0, 1, 0, 0, 0, 0],
    r = f.filter((_, i) => b[i]);

console.log(r);

Sign up to request clarification or add additional context in comments.

2 Comments

Desired resulting array ["A","C"]
i assume to filter the same place.
2

Assuming your f array is never longer than your b array

f.filter((item, index) => b[index] === 1);

If you're wanting this completely shorthand you can rename item and index and drop the === 1:

f.filter((a, i) => b[i]);

var f = ["A","B","C","D","E","F"]; //might be less than 8
var b = [1,0,0,1,0,0,0,0];   //always 8 elements
console.log(f.filter((a, i) => b[i]));

2 Comments

Desired resulting array ["A","C"]
@RomanPerekhrest in my answer I state: "...and assuming you meant D instead of C".
1

var f = ["A","B","C","D","E","F"];
var b = [1,0,0,1,0,0,0,0];

var res = f.filter(function(e, i) {
  return b[i]; // short for return b[i] === 1;
});

console.log(res);

Or even shorter using arrow functions like this:

var f = ["A","B","C","D","E","F"];
var b = [1,0,0,1,0,0,0,0];

var res = f.filter((e, i) => b[i]);

console.log(res);

5 Comments

Desired resulting array ["A","C"]
@RomanPerekhrest I've noticed and I'm still waiting for the user to clarify! It's is probably just a typo!
@RomanPerekhrest It would be totally pointless if the desired result would be [A,C].
He just re-editted the question It was a typo! As Kind user said there is no logic for it to be [A, C]!
@Kinduser, everyone realized that i was [A,C](not big deal). The OP should have presented the proper conditions
0

Another Way :

$(function(){
  f = ["A","B","C","D","E","F"];
  b = [1,0,0,1,0,0,0,0];
  x = [];
  $.each(b,function(key, value){
    value?x.push(f[key]):'';
  });
  console.log(x)
});

7 Comments

jquery goes always, but why?
@NinaScholz He is probably a jQuery-phobic needsmorejquery.com!
@ibrahimmahrir :))
@ibrahimmahrir, more the opposite, jqueryphilic
@NinaScholz wanna stop analyzing me ? :-D
|
0

Since for loops are faster than filter method i suggest this:

var results = [];
for(var i=0;i<b.length;i++){
    if (b[i]) results.push(f[i]);
}

1 Comment

you don't need == 1 with truthy values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.