AmyH
AmyH

Reputation: 23

Javascript - Compare substring from Array 1 to strings in Array 2 > Discard if multiple instances

I have two arrays. one array has sentences like so:

var sentences = [
"Bob goes to the zoo",
"Jim goes to the airport",
"Jenny and Bob go to the beach",
"Jenny goes to the cafe"
]

and the other has names like thus:

var names = [
"Bob",
"Jim",
"Jenny"
]

What i want to do is out put to a new array but only those strings that have only one instance from the names array. (so in this example it should leave out the "Jenny and Bob go to the beach" string.

Example output:

var output = [
"Bob goes to the zoo",
"Jim goes to the airport",
"Jenny goes to the cafe"
]

I know how to discard an array element if only one instance is present but not quite sure how to go about checking for multiple instances like required here :/

Upvotes: 0

Views: 71

Answers (3)

guest271314
guest271314

Reputation: 1

You can use for loop, Array.prototype.join(), String.prototype.match()

var sentences = [
"Bob goes to the zoo",
"Bob goes to the oz",
"Jim goes to the airport",
"Jim goes to the porch",
"Jenny and Bob go to the beach",
"Jenny goes to the cafe"
]

var names = [
"Bob",
"Jim",
"Jenny"
];

for (var i = 0, s = sentences.join("\n") + "\n", res = []; 
     i < names.length; i++) {
  res.push(s.match(new RegExp(".*(" + names[i] + ").*(?=\n)"))[0])
};

console.log(res);

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115222

Use Array#filter method

var sentences = [
    "Bob goes to the zoo",
    "Jim goes to the airport",
    "Jenny and Bob go to the beach",
    "Jenny goes to the cafe"
  ],
  names = [
    "Bob",
    "Jim",
    "Jenny"
  ];


var res = sentences.filter(function(v) { // iterate over sentence array to filter
  return names.filter(function(v1) { // iterate over names array and filter
    return v.indexOf(v1) > -1; // get elements which contains in the sentence
  }).length == 1; // filter based on the filtered names array length
});

console.log(res);


With ES6 arrow function

var sentences = [
    "Bob goes to the zoo",
    "Jim goes to the airport",
    "Jenny and Bob go to the beach",
    "Jenny goes to the cafe"
  ],
  names = [
    "Bob",
    "Jim",
    "Jenny"
  ];


var res = sentences.filter(v => names.filter(v1 => v.indexOf(v1) > -1).length === 1);

console.log(res);


UPDATE : In case if you want to match the entire word then it can be fixed by a simple variation with help of String#split method.

var sentences = [
    "Bob goes to the zoo",
    "Jim goes to the airport",
    "Jenny and Bob go to the beach",
    "Jenny goes to the cafe",
  "Patrick says hello to Patricia"
  ],
  names = [
    "Bob",
    "Jim",
    "Jenny",
    "Pat",
    "Patricia"
  ];


var res = sentences.filter(function(v) {
  return names.filter(function(v1) {
    // split based on space for exact word match
    return v.split(/\s+/).indexOf(v1) > -1;
  }).length == 1;
});

console.log(res);

Upvotes: 5

kazenorin
kazenorin

Reputation: 1465

Can make use of filter, split and reduce

var sentences = [
"Bob goes to the zoo",
"Jim goes to the airport",
"Jenny and Bob go to the beach",
"Jenny goes to the cafe"
];

var names = [
"Bob",
"Jim",
"Jenny"
];

// Filter out the sentences
var output = sentences.filter(function(sentence){

  // For each sentence, find number of times of appearance for each name
  return sentence.split(" ").reduce(function(count, word){
    if(names.indexOf(word) >= 0) {
      count += 1;
    }
    return count;
  }, 0) === 1;
});

console.log(output);

Upvotes: 0

Related Questions