Search code examples
javascriptnode.jsunderscore.js

How can I return an object out of an array?


I have an array of objects and I wanted to search through each object and check if the data matches a given value and I want to return the object out of the array and if it is not found then I have to return undefined.

This is what I have so far:

var data = [{
  id: 1,
  firstName: 'John',
  lastName: 'Smith'
}, {
  id: 2,
  firstName: 'Jane',
  lastName: 'Smith'
}, {
  id: 3,
  firstName: 'John',
  lastName: 'Doe'
}];
var asf[] = data[0];
return asf;

I'm trying to return the object if the condition in if else statement matches but it gives me error in returning array object.

I am also trying to use _.findwhere(data, pid) which is method in module of underscore can I use it to return the object out of array?


Solution

  • I'm not sure if you want to return the object or remove the object so I'll show you how to do both as both are very simple to do.

    This is a tidied version of your data:

    // this is your data
    var data = [{
      id: 1,
      firstName: 'John',
      lastName: 'Smith'
    }, {
      id: 2,
      firstName: 'Jane',
      lastName: 'Smith'
    }, {
      id: 3,
      firstName: 'John',
      lastName: 'Doe'
    }];
    

    This the loop you'll use to return the target object from the array:

    // loop through the data array
    for(var i = 0; i < data.length; i++) {
      // check if the current item is "John Smith"
      if(data[i].firstName == "John" && data[i].lastName == "Smith") {
        return data[i];
      }
      // continue with the loop if the current item is not "John Smith"
      continue;
    }
    

    This snippet does the exact same thing but without the continue:

    // loop through the data array
    for(var i = 0; i < data.length; i++) {
      // check if the current item is "John Smith"
      if(data[i].firstName == "John" && data[i].lastName == "Smith") {
        return data[i];
      }
    }
    

    This the loop you'll use to remove the target object from the array:

    // loop through the data array
    for(var i = 0; i < data.length; i++) {
      // check if the current item is "John Smith"
      if(data[i].firstName == "John" && data[i].lastName == "Smith") {
        delete data[i];
        // you can also use Array.prototype.splice() to remove an item from an array:
        // data.splice(i, 1);
      }
      // continue with the loop if the current item is not "John Smith"
      continue;
    }
    

    This snippet does the exact same thing but without the continue:

    // loop through the data array
    for(var i = 0; i < data.length; i++) {
      // check if the current item is "John Smith"
      if(data[i].firstName == "John" && data[i].lastName == "Smith") {
        delete data[i];
        // you can also use Array.prototype.splice() to remove an item from an array:
        // data.splice(i, 1);
      }
    }
    

    Use this snippet if you are using jQuery, instead of returning or deleting anything you can handle the object however you please inside the jQuery callback function.
    In this case, I'll be using console.log(); as an example:

    $.each(data, function(i, object) {
      if(object.firstName == "John" && object.lastName == "Smith") {
        console.log(object);
      }
    });
    

    Good luck and all the best.