skip to Main Content

Here are two arrays of the objects I have in js:

var arr_A = [
  {Name: "Smith", CheckedOut: true},
  {Name: "Jane", CheckedOut: true},
]

var arr_B = [
  {Name: "Smith", CheckedIn: true},
  {Name: "Paul", CheckedIn: true},
  {Name: "Wagen", CheckedIn: true},
]

If duplicates are found, I want to set CheckedOut: true, CheckedIn: true.

If it’s NOT a duplicate, I want to set either CheckedOut: false, CheckedIn: true OR CheckedOut: true, CheckedIn: false depending on their attribute’s values.

This is the estimated result from the example:

[
  {Name: "Smith", CheckedOut: true, CheckedIn: true},
  {Name: "Jane", CheckedOut: true, CheckedIn: false},
  {Name: "Paul", CheckedOut: false, CheckedIn: true},
  {Name: "Wagen", CheckedOut: false, CheckedIn: true},
]

This is what I have done:

let result = [];

for(var i=0; i<arr_B.length; i++){
  for(var j=0; j<arr_A.length; j++) {
    if(arr_A[j].Name == arr_B[i].Name) {
      result.push({
         Name: arr_A[j].Name,
         CheckedIn: true,
         CheckedOut: true
        })
    } else {
      if(arr_A[j].CheckedOut){
       result.push({
         Name: arr_A[j].Name,
         CheckedOut: true,
         CheckedIn: false
       })
      } else {
         result.push({
           Name: arr_A[j].Name,
           CheckedOut: false,
           CheckedIn: true
       })
      }
    }

  }
}

5

Answers


  1. You could take an object for collecting and initialize each value with default properties.

    const
        array1 = [{ Name: "Smith", CheckedOut: true }, { Name: "Jane", CheckedOut: true }],
        array2 = [{ Name: "Smith", CheckedIn: true }, { Name: "Paul", CheckedIn: true }, { Name: "Wagen", CheckedIn: true }],
        fn = (r, o) => {
            Object.assign(r[o.Name] ??= { Name: '', CheckedOut: false, CheckedIn: false }, o);
            return r;
        },
        result = Object.values(array2.reduce(fn, array1.reduce(fn, {})));
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
  2. You can use a combination of map, find, and conditional statements to iterate through both arrays and update the properties accordingly.

    concat method is used to combine the items that are not duplicates from arrB.

    function mergeArrays(arrA, arrB) {
      return arrA.map(itemA => {
        const foundItemB = arrB.find(itemB => itemB.Name === itemA.Name);
    
        if (foundItemB) {
          // If duplicate is found
          return {
            Name: itemA.Name,
            CheckedOut: true,
            CheckedIn: true,
          };
        } else {
          // If not a duplicate
          return {
            Name: itemA.Name,
            CheckedOut: itemA.CheckedOut,
            CheckedIn: !itemA.CheckedOut,
          };
        }
      }).concat(
        arrB.filter(itemB => !arrA.find(itemA => itemA.Name === itemB.Name))
               .map(itemB => ({
                 Name: itemB.Name,
                 CheckedOut: false,
                 CheckedIn: itemB.CheckedIn,
               }))
      );
    }
    
    
    let arr_A = [
      { Name: "Smith", CheckedOut: true },
      { Name: "Jane", CheckedOut: true },
    ];
    
    let arr_B = [
      { Name: "Smith", CheckedIn: true },
      { Name: "Paul", CheckedIn: true },
      { Name: "Wagen", CheckedIn: true },
    ];
    
    
    //result
    let result = mergeArrays(arr_A, arr_B);
    console.log(result);
    Login or Signup to reply.
  3. Here is a code snippet you can use to achieve the result you are looking for:

    var arr_A = [
      { Name: "Smith", CheckedOut: true },
      { Name: "Jane", CheckedOut: true }
    ];
    
    var arr_B = [
      { Name: "Smith", CheckedIn: true },
      { Name: "Paul", CheckedIn: true },
      { Name: "Wagen", CheckedIn: true }
    ];
    
    let result = [];
    
    let mapB = {};
    arr_B.map(item => {
      mapB[item.Name] = { CheckedIn: item.CheckedIn };
    });
    
    arr_A.map(itemA => {
      if (mapB[itemA.Name]) {
        result.push({ Name: itemA.Name, CheckedOut: true, CheckedIn: true });
        delete mapB[itemA.Name];
      } else {
        result.push({ Name: itemA.Name, CheckedOut: true, CheckedIn: false });
      }
    });
    
    for (let name in mapB) {
      result.push({ Name: name, CheckedOut: false, CheckedIn: true });
    }
    
    console.log(result);
    Login or Signup to reply.
  4. First, use Set() to get an array of unique names. Then, use map to build an object for each of the names, where you extract the properties you need from each of the source arrays:

    const A = [
      {Name: "Smith", CheckedOut: true},
      {Name: "Jane", CheckedOut: true},
    ]
    
    const B = [
      {Name: "Smith", CheckedIn: true},
      {Name: "Paul", CheckedIn: true},
      {Name: "Wagen", CheckedIn: true},
    ]
    
    const names = [...new Set([A, B].flatMap(i => i.map(j => j.Name)))]
    
    const result = names.map(Name => ({
      Name,
      CheckedOut: A.some(i => i.Name===Name && i.CheckedOut),
      CheckedIn:  B.some(i => i.Name===Name && i.CheckedIn)
    }))
    
    console.log(result)
    Login or Signup to reply.
  5. Use an object like userName: {Name:..., CheckedIn:... CheckedOut:...} to accumulate values:

    let diff = {}
    
    for (let r of arr_A) {
        diff[r.Name] = {Name: r.Name, CheckedIn: false, CheckedOut: true}
    }
    
    for (let r of arr_B) {
        if (r.Name in diff)
            diff[r.Name].CheckedIn = true
        else
            diff[r.Name] = {Name: r.Name, CheckedIn: true, CheckedOut: false}
    }
    

    After that, Object.values(diff) will return the data you’re looking for.

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