0

After some web scraping I have an array of names called someObjArr.

Array looks like this: [ { name: Joe 'Panik' } ]

I want to check if a name exists in this array and here is my code:

For Example:

        var someObjArr = [];

        var filter = $('a.player-link').each(function(i, element) {
        var text = $(this).text();
        if(someObjArr[i] == undefined){
            someObjArr[i] = {};
        };
        someObjArr[i].name = text;

        function nameExistsInArray(name, array) {
            for(var i = 0; i < array.length; i++)(function(obj) {
                if(obj.name == name) {
                    return true;
                }
            })
            return false;
        };
            var exists = nameExistsInArray('Joe Panik', [{ name: 'Joe Panik' }]);
            console.log(exists);
        });

The output from this script return false fir every item in the array.

CiscoKidx
  • 922
  • 8
  • 29
  • because you compare an Object with a String! – Jeff Jul 25 '15 at 21:47
  • How would I compare an object in this situation? – CiscoKidx Jul 25 '15 at 21:49
  • that might help: [check-if-object-is-already-present-in-array](http://stackoverflow.com/questions/31453512/check-if-object-is-already-present-in-array) – Jeff Jul 25 '15 at 21:49
  • Sweet i'll check it out. Thanks! – CiscoKidx Jul 25 '15 at 21:50
  • Changed my OP to reflect suggestions made from comments. – CiscoKidx Jul 25 '15 at 22:05
  • 1
    I dont exactly get the poit what are you trying to do here if you have objects why you are trying to put them into an array and anyway without stringifying them you cannot do that way if you explain mod eclear what is the aim here exactly i will try to help – nikoss Jul 25 '15 at 22:08
  • 1
    right now you don't ask if the `personExists` from anywere outside that function. – Jeff Jul 25 '15 at 22:09
  • 1
    Looks like `var check = personExists([{Name: 'Joe Panik'}]);` is dead code, it is preceded by a `return` statement, You will certainly benefit by formatting your code, and by using meaninful variable names – Wand Maker Jul 25 '15 at 22:10
  • I am trying to use someObjArr to validate names from another scrape before inserting into SQL database – CiscoKidx Jul 25 '15 at 22:13
  • Thank you for all of the comments unfortunately I still don't understand! I am too new to javascript and coding in general. – CiscoKidx Jul 25 '15 at 22:13
  • "Oh, and you convert the array into a Boolean and then call length on it." Thanks. What do you mean? – CiscoKidx Jul 25 '15 at 22:27
  • I just want "check" to return 1 if it is present or 0 if not. – CiscoKidx Jul 25 '15 at 22:28
  • To repeat Wand Maker, you first need to move `var check = personExists([{name: 'Joe Panik'}]);` and `console.log(check);` out of the `personExists` function or nothing will happen. – thoughtrepo Jul 25 '15 at 22:32
  • @sails4life - You are not supposed to add your final working answer to your question. That is not how StackOverflow works. If the answer you went with was offered here already, then you just check the green checkmark to the left of that answer to indicate your question is now answered and to show which one you selected. If you came up with your own answer, then you add your own answer (not in your question). – jfriend00 Jul 25 '15 at 23:08
  • `for (...)(function () { ... })` what it does? – sigod Jul 25 '15 at 23:19

1 Answers1

2

How about something like this?

function nameExistsInArray(name, array) { 
  for(var i = 0; i < array.length; i++) {
    if(array[i].Name == name) {
      return true;
    }
  }
  return false;
};

Usage:

var exists = nameExistsInArray('Joe Panik', [{ Name: 'Joe Panik' }]);

Explanation: You want to determine if some name of type string is equal to one of the Name properties of the objects in an array.

In our function, we'll pass a string name and an array of objects with Name properties. Then, we'll iterate through each obj in array using a for loop, and then we'll access the Name property and compare it with the name argument. When we see it, return true, but if we've iterated through every element and haven't found a match, we'll return false.

heartyporridge
  • 1,151
  • 8
  • 25
  • 1
    `Array.forEach` uses a closure. In your example, `nameExistsInArray` will always return `false`. Use a normal `for` loop. – thoughtrepo Jul 25 '15 at 22:37
  • I updated my OP with your code. I had to add a little because obj is undefined in yours. Unfrtunatly it still produces a false for each item in the array. – CiscoKidx Jul 25 '15 at 22:53
  • My one question would be is there a way to make it return a single true or false as opposed to one for each loop – CiscoKidx Jul 25 '15 at 23:05
  • The function actually *does* return a single `true` or `false` value. That's because it will only return if the `if` statement conditional is met, or if the loop is over. – heartyporridge Jul 25 '15 at 23:08
  • It returns one true or false for your exmaple but in my array I have 300 people it it. It checks each one and returns a true or false. – CiscoKidx Jul 25 '15 at 23:10