1

If I have an array like this:

arr = ["hey", "some", "string", "aaa", "ccc"];

And I want to compare if there is same string in array but with a special rule like this:

if arr[1] == "some"

arr = ["hey", "bbb", "string", "aaa", "ccc"];    //return true
arr = ["hey", "some", "string", "aaa", "bbb"];   //return true
arr = ["hey", "some", "string", "aaa", "some"];  //return false

I've already know how to compare string with this code:

var arr = ["hey", "some", "string", "aaa", "ccc"];
var val = $('#somethingFromTableForm').val();
for(var i = 0; i < arr.length; i++) {
    if (arr[i].indexOf(val) > -1){
        alert("same string");
        return false;
    }
}
return true;

But how to compare string with this special rule?

S.P.
  • 369
  • 6
  • 18
  • 3
    Can you explain what this rule is? – Rajesh Feb 21 '17 at 07:27
  • Not sure what "special rule" is? Why does first example evaluate to `true`, second example to `true`, and third example `false`? – guest271314 Feb 21 '17 at 07:27
  • try this function : jQuery.inArray(variable_name,your_array_arr) – Yasin Patel Feb 21 '17 at 07:29
  • Is it about checking if array has duplicates? – Rajesh Feb 21 '17 at 07:30
  • I mean if arr[1] == "some", and yes, the arr[1] value is "some", the comparing function shouldn't check arr[1] itself but other value in array. – S.P. Feb 21 '17 at 07:32
  • Is requirement for `"some"` to be unique in array, and only at index `1` of array? – guest271314 Feb 21 '17 at 07:34
  • @S.P. Please try to explain in layman terms in basic english. Dont go technical. – Rajesh Feb 21 '17 at 07:36
  • I actually want to do is if there's a table and people can write down they're name on it, and first people wrote down "Charlie", the rest of people couldn't write "Charlie", my code runs perfectly on this part. But I also create a form that people can edit they're name, so if first people goes into edit zone, he thought about which name to change, but he gave up change his name and he want to quit the edit zone, but my code return false for him, I want to avoid this situation cause "Charlie" is already in the array. – S.P. Feb 21 '17 at 07:39
  • So to be clear, if a user saves name (*normal submit or edit*), you have to check if value exists in array, then return false as pushing it will make it duplicate. Am I right? – Rajesh Feb 21 '17 at 07:48
  • 1
    in this case, you could check the new name with simply `indexOf`. – Nina Scholz Feb 21 '17 at 07:49
  • @Rajesh yes you're right. My code works perfect when first submit, but it return false after "Charlie" goes to edit zone, but actually he did nothing but it return false for him. :( – S.P. Feb 21 '17 at 07:57
  • Just check for `names.indexOf(newName) === -1`. If this returns false, value exists in array and you should not push. If true is returned, you can safely push it – Rajesh Feb 21 '17 at 08:01

1 Answers1

0

You could use Array#indexOf and Array#lastIndexOf and a check for equality for every element with Array#every.

function check(array) {
    return array.every(function (a, i, aa) {
        return i === aa.lastIndexOf(a);
    });
}

console.log(check(["hey", "bbb", "string", "aaa", "ccc"]));   // true
console.log(check(["hey", "some", "string", "aaa", "bbb"]));  // true
console.log(check(["hey", "some", "string", "aaa", "some"])); // false
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • If the question is about checking duplicates, i guess we should mark it as dupe. [#reference link](http://stackoverflow.com/questions/19655975/check-if-an-array-contains-duplicate-values). Haven't done myself yet as question is unclear – Rajesh Feb 21 '17 at 07:31
  • Just a pointer, you can just do `aa.filter(a).length === 1`. `indexOf` and `lastIndexOf` both will loop over array – Rajesh Feb 21 '17 at 07:41
  • @Rajesh, but filter has no early exit. – Nina Scholz Feb 21 '17 at 07:43
  • 1
    My bad, I meant inside `.every( (x,i,a) => a.filter(x).length === 1 )`. But your current update solves it. :-) – Rajesh Feb 21 '17 at 07:45