0

Lets say there are two javascript arrays. What I want to find out is how to take the second array and find out if there is another person that has the same favoriteColor as someone in the first array. If there is I want it to switch arrays. They keep the same position as the person they are switching with. Anyone know how that can be done.

var array1 = [
    {
        name: 'Person 1',
        favoriteNumber: '1',
        hairColor: 'Brown',
        strongHand: 'Left'
    },
    {
        name: 'Person 2',
        favoriteNumber: '2',
        hairColor: 'Brown',
        strongHand: 'Left'
    },
    {
        name: 'Person 3',
        favoriteNumber: '3',
        hairColor: 'Blonde',
        strongHand: 'Right'
    }

var array2 = [
    {
        name: 'Person 4',
        favoriteNumber: '2',
        hairColor: 'Blonde',
        strongHand: 'Right'
    },
    {
        name: 'Person 5',
        favoriteNumber: '22',
        hairColor: 'Blonde',
        strongHand: 'Right'
    }
user3732216
  • 1,579
  • 8
  • 29
  • 54

2 Answers2

1

I'd say just a nested for loop. It would look something like this:

for (let i = 0; i < array1.length; i++) {
    for (let i2 = 0; i2 < array2.length; i2++) {
        if (array1[i].favoriteNumber == array2[i2].favoriteNumber) {
            // swap the two people
            let tmp = array2[i2];
            array2[i2] = array1[i];
            array1[i] = tmp;

            break; // found a match, so skip on to the next i value
        }
    }
}
Spooky
  • 1,752
  • 21
  • 37
  • Basically this is going to be an action button code block. Once the icon is clicked to switch with another person it will switch it out with the person with the same favoriteNumber in the first array. – user3732216 Jul 03 '17 at 00:45
0

Apparently, the idea is to switch objects in two arrays with each other when they share the same favorite number. To accomplish this feat, a for-loop needs to iterate over every object in the first array. Comparing each object with the objects in the second array is most efficient with a nested for-loop. The swapping is especially easy if one utilizes a cool feature that eliminates having to swap with a temporary variable:

ES6 (Firefox and Chrome already support it (Destructuring Assignment Array Matching))

(See this discussion.)

var a = [
  {
    name: "Person 1",
    favoriteNumber: "22",
    hairColor: "Brown",
    strongHand: "Left"
  },
  {
    name: "Person 2",
    favoriteNumber: "2",
    hairColor: "Brown",
    strongHand: "Left"
  },
  {
    name: "Person 3",
    favoriteNumber: "3",
    hairColor: "Blonde",
    strongHand: "Right"
  }
];

var b = [
  {
    name: "Person 4",
    favoriteNumber: "2",
    hairColor: "Blonde",
    strongHand: "Right"
  },
  {
    name: "Person 5",
    favoriteNumber: "22",
    hairColor: "Blonde",
    strongHand: "Right"
  }
];

function switchOnFav() {
  for (let i = 0, max = 3; i < max; i++) {
    for (let j = 0, mx = 2; j < mx; j++) {
      if (a[i]["favoriteNumber"] == b[j]["favoriteNumber"]) {
        [a[i], b[j]] = [b[j], a[i]]; // ECMS 6
      }
    }
  }
}

function showResults(){
  for (let r = 0, m = 3; r < m; r++) {
    console.log( "a[" + r + "]: " + a[r].name);
  }
  for (let s = 0, x = 2; s < x; s++) {
    console.log( "b[" + s + "]: " + b[s].name);
  }
}

d = document;
d.g = d.getElementById;
var sw = d.g("switch");
var res = d.g("show");

sw.addEventListener("click",switchOnFav);
res.addEventListener("click",showResults);
button {
background: #00f;
color:#fff;
width:120px;
height:40px;
}
<button id="switch">Switch</button>

<button id="show">Show Results</button>
slevy1
  • 3,797
  • 2
  • 27
  • 33