user1765862
user1765862

Reputation: 14145

is equal on object property inside array

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?

Upvotes: 1

Views: 82

Answers (2)

ncubica
ncubica

Reputation: 8485

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");
    }
});

Upvotes: 1

TbWill4321
TbWill4321

Reputation: 8666

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' ) );

Upvotes: 1

Related Questions