0

I have two arrays defined as follows,

var first = [true, false, true, false]; 
var second = [true, true, false, false];

I need the output as [true, false, false, false]

e.g. logically AND each index values from the arrays one by one and display output.

How can I do this with ES6 methods?

8
  • var third = [true, false]. More serioulsy, why should the output be like that? You didn't specify how this algorithm should work. Commented Jan 19, 2018 at 11:54
  • I need the output as [true, false] Why? How is this output arrived? Commented Jan 19, 2018 at 11:56
  • added the reason. Commented Jan 19, 2018 at 11:59
  • 1
    @parkme - change it to var first = [true, false, true, false]; and var second = [true, true, false, false]; and the add the extra info to the expected output. That will stop the uncertainty and bickering. Commented Jan 19, 2018 at 12:01
  • 1
    @archer updated Commented Jan 19, 2018 at 12:03

1 Answer 1

2

You can use .map and do a comparison by passing index to the second

DEMO

var first = [true,false];
var second = [true,true];

var result =  first.map((aVal, aIndex) => aVal && second[aIndex]);

console.log(result);

EDIT

var first = [true, false, true, false]; 
var second = [true, true, false, false];
var result =  first.map((aVal, aIndex) => aVal && second[aIndex]);
console.log(result);

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

8 Comments

Are you sure? Because from the question it's not clear they need to && the array's elements. It's not specified. Or maybe I'm just overthinking it.
@FedericoklezCulloca Well, I think it's the only logical assumption based on the inputs and the output. But I agree that the author should've made that clear.
@tiagodws that's the reason I'm not downvoting this answer.
It could be equality, the OP does say compare.. So false,false = true.. but false && false = false.. So a boolean and isn't the only logical assumption.
OP has clarified. He means == not &&
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.