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?

1
  • 2
    Use .some() Commented Nov 24, 2015 at 23:05

2 Answers 2

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' ) );
Sign up to request clarification or add additional context in comments.

5 Comments

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
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.
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 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
OP explicitly asked to check for presence of equality on a property, that's what I did.
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?
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");
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.