skip to Main Content

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


  1. You can use + to coerce true/false into 1/0. This approach uses destructuring to extract the first element as i and the remaining elements as j.

    const 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]];
    
    const b = ['AED','AFN','AMD']
    
    const out = a.map(([i, ...j]) => [i, ...j, +b.includes(i)])
    
    console.log(out)

    or, to alter the original array directly:

    const 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]];
    
    const b = ['AED','AFN','AMD']
    
    a.forEach(i => i.push(+b.includes(i[0])))
    
    console.log(a)
    Login or Signup to reply.
  2. A simple for loop that checks if b .includes() an item in a will do the trick. This mutates the original array.

    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"];
    
    for (var i = 0; i < a.length; i++)
      b.includes(a[i][0]) ? a[i].push(1) : a[i].push(0);
    
    console.log(a);
    Login or Signup to reply.
  3. 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"];
    
    for (var i = 0; i < a.length; i++) {
      if (b.includes(a[i][0])) {
        a[i].push(1);
      } else {
        a[i].push(0);
      }
    }
    
    console.log(a);
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search