Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

Converting Code to Demo
Source Link
dota2pro
  • 8k
  • 8
  • 52
  • 87

What is the best way to find if an object is in an array?

This is the best way I know:

function include(arr, obj) {
 for(var i=0; i<arr.length; i++) {
 if (arr[i] == obj) return true;
 }
}
include([1,2,3,4], 3); // true
include([1,2,3,4], 6); // undefined

function include(arr, obj) {
 for (var i = 0; i < arr.length; i++) {
 if (arr[i] == obj) return true;
 }
}
console.log(include([1, 2, 3, 4], 3)); // true
console.log(include([1, 2, 3, 4], 6)); // undefined

What is the best way to find if an object is in an array?

This is the best way I know:

function include(arr, obj) {
 for(var i=0; i<arr.length; i++) {
 if (arr[i] == obj) return true;
 }
}
include([1,2,3,4], 3); // true
include([1,2,3,4], 6); // undefined

What is the best way to find if an object is in an array?

This is the best way I know:

function include(arr, obj) {
 for (var i = 0; i < arr.length; i++) {
 if (arr[i] == obj) return true;
 }
}
console.log(include([1, 2, 3, 4], 3)); // true
console.log(include([1, 2, 3, 4], 6)); // undefined

deleted 194 characters in body
Source Link
Michał Perłakowski
  • 93.4k
  • 30
  • 165
  • 189

Possible Duplicate:
array.contains(obj) in JavaScript

What is the best way to find if an object is in an array?

This is the best way I know:

function include(arr, obj) {
 for(var i=0; i<arr.length; i++) {
 if (arr[i] == obj) return true;
 }
}
include([1,2,3,4], 3); // true
include([1,2,3,4], 6); // undefined

Possible Duplicate:
array.contains(obj) in JavaScript

What is the best way to find if an object is in an array?

This is the best way I know:

function include(arr, obj) {
 for(var i=0; i<arr.length; i++) {
 if (arr[i] == obj) return true;
 }
}
include([1,2,3,4], 3); // true
include([1,2,3,4], 6); // undefined

What is the best way to find if an object is in an array?

This is the best way I know:

function include(arr, obj) {
 for(var i=0; i<arr.length; i++) {
 if (arr[i] == obj) return true;
 }
}
include([1,2,3,4], 3); // true
include([1,2,3,4], 6); // undefined
Post Closed as "Duplicate" by Michał Perłakowski javascript Users with the javascript badge or a synonym can single-handedly close questions as duplicates and reopen them as needed.
Post Reopened by Michał Perłakowski javascript Users with the javascript badge or a synonym can single-handedly close questions as duplicates and reopen them as needed.
changed the title because it was misleading before - OP didnt wanted to return the item, but rather check whether it was in an array
Source Link

Best way to find if an item is in a JavaScript array?

Best way to find if an item is in a JavaScript array?

Possible Duplicate:
array.contains(obj) in JavaScript

What is the best way to find if an object is in an array?

This is the best way I know:

function include(arr, obj) {
 for(var i=0; i<arr.length; i++) {
 if (arr[i] == obj) return true;
 }
}
include([1,2,3,4], 3); // true
include([1,2,3,4], 6); // undefined

Best way to find an item in a JavaScript array?

Possible Duplicate:
array.contains(obj) in JavaScript

What is the best way to find if an object is in an array?

This is the best way I know:

function include(arr, obj) {
 for(var i=0; i<arr.length; i++) {
 if (arr[i] == obj) return true;
 }
}
include([1,2,3,4], 3); // true
include([1,2,3,4], 6); // undefined

Best way to find if an item is in a JavaScript array?

Possible Duplicate:
array.contains(obj) in JavaScript

What is the best way to find if an object is in an array?

This is the best way I know:

function include(arr, obj) {
 for(var i=0; i<arr.length; i++) {
 if (arr[i] == obj) return true;
 }
}
include([1,2,3,4], 3); // true
include([1,2,3,4], 6); // undefined
insert duplicate link
Source Link
Loading
Post Closed as "exact duplicate" by casperOne
added 7 characters in body; edited title
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 110
  • 134
Loading
Question Protected by Marc Gravell
edited tags
Link
Adhip Gupta
  • 7.2k
  • 8
  • 36
  • 46
Loading
Source Link
zimbatm
  • 9.4k
  • 4
  • 23
  • 11
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /