I need to compare var and var b and if b is available in a i need to append 1 in the a and if it was false I need to add 0
var a=[["AED",1],["AFN",22.08086],["ALL",27.171554],["AMD",105.058803], ["ANG",0.487406],["AOA",227.493716],["ARS",95.308373],["AUD",0.420237],["AWG",0.487406], ["AZN",0.462914],["BAM",0.490727],["BBD",0.544588]];
var b=[AED,AFN,AMD]
compare a and b and if b code exist in a need to append 1 in the array
OUTPUT
[["AED",1,1],["AFN",22.08086,1],["ALL",27.171554,0],["AMD",105.058803,1] ["ANG",0.487406,0],["AOA",227.493716,0],["ARS",95.308373,0],["AUD",0.420237,0],["AWG",0.487406,0],["AZN",0.462914,0],["BAM",0.490727,0],["BBD",0.544588,0]]
3
Answers
You can use
+
to coerce true/false into 1/0. This approach uses destructuring to extract the first element asi
and the remaining elements asj
.or, to alter the original array directly:
A simple for loop that checks if
b
.includes()
an item ina
will do the trick. This mutates the original array.This code snippet will iterate through each sub-array in a, check if the currency code (first element) exists in the array b, and then push either 1 or 0 to the sub-array accordingly. The resulting array a will have the desired output as you’ve mentioned.