简体   繁体   中英

How to remove matching words from array?

I have two arrays..

Eg:

A = [ "uparea" , "selection"]

B = ["upareasetting"]

I want to print

"upareaselectionsetting"

Need to remove "uparea" in b ???

First loop through each item in A to check whether the current word exist in B . If exist then remove that from B . Finally concatenate and join both array's to get the expected output.

Try the following with forEach() , concat() and join() :

 let A = [ "uparea" , "selection"] let B = ["upareasetting"]; A.forEach(function(i){ if(B[0].indexOf(i) > -1) B = B[0].replace(i, ''); }); let res = A.concat(B).join(''); console.log(res); 

const A = ["uparea", "selection"],
  B = ["upareasetting"];

console.log(
  A.join("") +
  B.map(
    item => item.replace(/uparea/, "")
  ).join("")
);

// upareaselectionsetting

Here is my solution, hope it helps.

function removeMatchingWords(a, b) {
    result_string = "";
    for (let i=0; i<a.length; i++) {
        result_string += a[i];
        for (let j=0; j<b.length; j++) {
            if (b[j].includes(a[i])) {
                b[j] = b[j].replace(a[i], "");
            }
        }
    }
    for (let k=0; k<b.length; k++) {
        result_string += b[k];
    }
    return result_string;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM