1

I have object

var emailData = new {};
emailData.Id;
emailData.EmailAddress;

later in code I'm populating array with emailData objects

 var emailDataArr = new Array();
 emailDataArr.push(..someobject...);

how can I check does any object on property EmailAddress inside emailDataArr array is equal to some string?

user1765862
  • 13,635
  • 28
  • 115
  • 220
  • 2
    Use [`.some()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) – Bergi Nov 24 '15 at 23:05

2 Answers2

1

You can get that by using Array.prototype.some. Here's an example:

function hasAddress( array, address ) {
    return array.some(function(data) {
        return data.EmailAddress === address;
    });
}

var emailDataArr = new Array();
emailDataArr.push(/* Data */);
console.log( hasAddress( emailDataArr, 'SomeString' ) );
TbWill4321
  • 8,626
  • 3
  • 27
  • 25
  • this is not comparing against an string. if `data.EmailAdress` === null will return true. So while the idea is right, the checking is incorrect and is not answering the current question which is compare against `string` – ncubica Nov 25 '15 at 00:19
  • The OP asked to check if the array has addresses that "is equal to some string". He was not asking to make sure that they are `typeof string`. – TbWill4321 Nov 25 '15 at 00:21
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some ```some() executes the callback function once for each element present in the array until it finds one where callback returns a true value. If such an element is found, some() immediately returns true``` That's mean that if fine a match in the first element will not evaluate the rest of them, What you did is the same that go over the array and do `(item.indexOf(address) > -1)` Sorry but IMO the answer is wrong – ncubica Nov 25 '15 at 00:59
  • OP explicitly asked to check for presence of equality on a property, that's what I did. – TbWill4321 Nov 25 '15 at 01:06
  • This way I'm checking only last object in the array for particular string, is there a way to go trough all object and check for that value, like any in c# linq? – user1765862 Nov 25 '15 at 08:17
1

You can go to http://jsfiddle.net/du2b6r1y/ and see live example.

//https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/create
var emailData = {}; //Please use Object.create instead of new {}; if that not possible you simple should use {} 

// you can also 
var emailDataArr = [
    {
        Id: 1,
        EmailAdress : "nadf@asdfa"
    },
    {
        Id: 2,
        EmailAdress : "adfasd@asdfa"
    },
    {
        Id: 3,
        EmailAdress : null
    }
]; //please avoid new in all you javascript code if possible

var filter = emailDataArr.filter(function(email) {
return typeof email.EmailAdress === "string";
}); 

console.log(filter);
//you will get only emails that are strings
//[object, object];

//or you can go over each of them and compare them

emailDataArr.forEach(function(email) {
    if(typeof email.EmailAdress === "string") {
        //do your thing
        console.log("is a string");
    }
});
ncubica
  • 8,169
  • 9
  • 54
  • 72