2

I am trying to select all the elements in an Array that contain the word "Orange", independently of the number before.

I tried with this code, but it is only working when I write the exact element name like "02orange".

var DWArray = ["apple", "apple", "02orange", "03orange", "04orange", "potato"];

function checkOrange(orange) {
    return orange == "02orange";
}
var OrangeArray = DWArray.filter(checkOrange);
return OrangeArray.join(", ");

My desired result is:

["02orange", "03orange", "04orange"];

Mikev
  • 2,012
  • 1
  • 15
  • 27
Kayra
  • 83
  • 1
  • 7
  • I suggest that you google "javascript string". The first hit is the MDN documentation which has a list of available methods. Look through this list and find one that does what you need. – Code-Apprentice Feb 19 '19 at 21:06

4 Answers4

2

You could return the result of the check with String#includes.

function checkOrange(orange) {
    return orange.includes("orange");
}

var DWArray = ["apple", "apple", "02orange", "03orange", "04orange", "potato"],
    OrangeArray = DWArray.filter(checkOrange);

console.log(OrangeArray.join(", "));

For older Browser, you might use String#indexOf.

function checkOrange(orange) {
    return orange.indexOf("orange") !== -1;
}

var DWArray = ["apple", "apple", "02orange", "03orange", "04orange", "potato"],
    OrangeArray = DWArray.filter(checkOrange);

console.log(OrangeArray.join(", "));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

One option would be to use RegExp constructor and RegExp.prototype.test() with string "orange" passed as first parameter and i (ignore case; if u flag is also enabled, use Unicode case folding) and g flags (global match; find all matches rather than stopping after the first match) passed as second parameter

var DWArray = ["apple", "apple", "02orange", "03orange", "04orange", "potato"];

function checkOrange(orange) {
  return new RegExp("orange", "ig").test(orange)
}

var OrangeArray = DWArray.filter(checkOrange);

console.log(OrangeArray, OrangeArray.join(", "));
guest271314
  • 1
  • 15
  • 104
  • 177
  • Using RegExp I get this error: "cannot supply flags when constructing one regexp from another". Do you know why? Thank you – Kayra Feb 19 '19 at 21:21
  • @Kayra See updated post. Should be a string passed as first parameter to `RegExp` constructor – guest271314 Feb 19 '19 at 21:23
  • Is the `g` flag really useful since the OP only needs to know if the word contains `orange` ? +1 for the `i` flag though – jo_va Feb 19 '19 at 21:32
0

Use .includes to check if the string includes orange in it, with filter. Refer

var DWArray = ["apple", "apple", "02orange", "03orange", "04orange", "potato"];
console.log(DWArray.filter(e=>e.includes('orange')))
ellipsis
  • 12,049
  • 2
  • 17
  • 33
  • when I use "Includes" I get this error "orange.includes is not a function in ", that is why I used filter instead. Do you know why? Thank you – Kayra Feb 19 '19 at 21:22
  • You might find the answer to that here. https://stackoverflow.com/questions/41820770/using-includes-method-in-a-function – ellipsis Feb 19 '19 at 21:27
0

You can do it with a regex and RegExp.test() in one line:

const DWArray = ["apple", "apple", "02orange", "03orange", "04orange", "potato"];
const OrangeArray = DWArray.filter(s => /orange/i.test(s)).join(', ');

console.log(OrangeArray);
jo_va
  • 13,504
  • 3
  • 23
  • 47