1

I have an array called Cards that on console log looks like

 Array[3]
 0: Object
 1: Object
 2: Object

each of these has a name: which I am using to reference it want to find it reference the obejct I have

var FoundInArray = [];
 function CheckIndexArray(ArraySearch, array) {
 var i;
 for (i = 0; i < array.length; i++) {
 if (array[i].name === ArraySearch) {
 FoundInArray = array[i];
 return true
 break
 } else {return false;} }
 }
 if (CheckIndexArray(document.getElementById("userInput").value, Cards) == true) {
 console.log(FoundInArray);

the 3 names are ADAM, Arron Shamira, Adrian Omarsh this however only finds ADAM if I search the other two it doesn't find them. The question is why???

asked Nov 1, 2016 at 10:32
1
  • You are returning false, thats why. Commented Nov 1, 2016 at 10:35

2 Answers 2

1

As commented before, your for loop breaks after 1st iteration because of else {return false;}

Also, you can look into Array.find() or Array.filter.

array.find

This will return first matching value or undefined

var arr = [
 {name: 'foo', id:1},
 {name: 'foo', id:2},
 {name: 'bar', id:3},
 {name: 'test', id:4}
]
var searchName = "foo";
var r = arr.find(function(x){ return x.name === searchName});
console.log(r)

array.filter

This will return you all matching values or []

var arr = [
 {name: 'foo', id:1},
 {name: 'foo', id:2},
 {name: 'bar', id:3},
 {name: 'test', id:4}
]
var searchName = "foo";
var r = arr.filter(function(x){ return x.name === searchName});
console.log(r)

Also if the purpose is only to check the existence of some value, you can even try array.some

Array.some

Unlike previously mentioned methods, this returns a boolean value. If you do not have use for value, you can even use this method.

var arr = [
 {name: 'foo', id:1},
 {name: 'foo', id:2},
 {name: 'bar', id:3},
 {name: 'test', id:4}
]
var searchName = "foo";
var isAvailable = arr.some(function(x){ return x.name === searchName});
console.log(isAvailable)
searchName = "foo123";
isAvailable = arr.some(function(x){ return x.name === searchName});
console.log(isAvailable)

answered Nov 1, 2016 at 10:37
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks That totally helped me past the section i was on and you explained it really well also the code snippets helped a lot thanks again
0

the problem is the return false, it breaks the loop.

also there are better ways of filtering an array like array.filter() or array.find()

src: .filter, .find

answered Nov 1, 2016 at 10:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.