简体   繁体   中英

Loop through two arrays in Javascript to search an object by value

I'm trying to loop through two arrays to find an id from the first that matches numbers from the second. Right now I'm getting either an infinite loop (wheeee!) or the entire first array. When I hard code the id number I want it returns the organization that I want. I made up simple data to play with the loops by the way.

Code:

var clients = [{
    "id": 1,
    "organization": "Sir Barks a lot"
  },
  {
    "id": 2,
    "organization": "Wag the dog daycare"
  },
  {
    "id": 3,
    "organization": "Purfect pet sitters"
  }
];

var index = [1, 7, 8];
var orgName = [];

for (var i = 0; i < clients.length; i++) {
  for (var y = 0; y <= index.length; y++) {
    if (clients[i].id == [y]) {
      orgName.push(clients[i].organization);
    }
  }
}
console.log(orgName);

You've got an error in your comparison:

`clients[i].id === [y]` should be `clients[i].id === index[y]`

 var clients = [{"id":1,"organization":"Sir Barks a lot"},{"id":2,"organization":"Wag the dog daycare"},{"id":3,"organization":"Purfect pet sitters"}]; var index = [1, 7, 8]; var orgName = []; for (var i = 0; i < clients.length; i++) { for (var y = 0; y < index.length; y++) { if (clients[i].id === index[y]) { // you need to compare it to the index in the y place, and not to y or [y] orgName.push(clients[i].organization); } } } console.log(orgName); 

Another approach is to create an object map using Array#reduce from the 1st array, and then Array#reduce the 2nd array, and take the values from the object map:

 var clients = [{"id":1,"organization":"Sir Barks a lot"},{"id":2,"organization":"Wag the dog daycare"},{"id":3,"organization":"Purfect pet sitters"}]; var index = [1, 7, 8]; var clientsMap = clients.reduce(function(m, o) { m[o.id] = o; return m; }, Object.create(null)); var result = index.reduce(function(r, i) { clientsMap[i] && r.push(clientsMap[i].organization); return r; }, []); console.log(result); 

Matt had the simplest answer, the above works great when I remember to that I'm trying to get y of index.

if (clients[i].id == index[y]) and not if (clients[i].id == [y])

I've made two simple changes to your code that were causing you errors.

for (var y = 0; y < index.length; y++) {

When looping through an array, you want to iterate your loop from 0 to the length of the array-1 (also known as 0 to the length of your array exclusive ). This a very common practice that will help to mitigate off by one errors in your code.

if (clients[i].id == index[y])

In your code, you are comparing the value of y to the client id at position 'i' in your array. What you want to do instead is compare the value of index at the 'y' position in your array. Otherwise, you are simply checking if clients[i].id is equal to 0, 1, 2, etc.

var clients = [{
    "id": 1,
    "organization": "Sir Barks a lot"
  },
  {
    "id": 7,
    "organization": "Wag the dog daycare"
  },
  {
    "id": 3,
    "organization": "Purfect pet sitters"

  }
];

var index = [1, 7, 8];

var orgName = [];
for (var i = 0; i < clients.length; i++) {
  for (var y = 0; y < index.length; y++) {
    if (clients[i].id == index[y]) {
      orgName.push(clients[i].organization);
    }
  }
}
window.alert(orgName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM