Skip to content Skip to sidebar Skip to footer

Javascript/jquery Find And Delete Array Value In The Rest Of Array

Ok, not sure how to ask this question, but I'm trying to gather values into an array, but each value MAY have duplicated (or appended) strings on the rest of the values and I belie

Solution 1:

This builds a new array using each string in the old array after first replacing, in that string, each string in the new array with empty string.

var arr = [
  'This is a string.',
  "here's another string. This is a string.",
  "And now there's a third string.  here's another string. This is a string.",
  "Here's a string that might not follow the pattern.  This is a string."
];

var out = arr.reduce(function(w, s) {
  w.push(w.reduce(function(s1, e) {
    return s1.replace(e, "");
  }, s).trim());
  return w;
}, []);

alert(out);

Solution 2:

var strings = [
    "This is a string.",
    "here's another string. This is a string.",
    "And now there's a third string.  here's another string. This is a string.",
    "Here's a string that might not follow the pattern.  This is a string."
];

var i, j, pos;
for (i = 0; i < strings.length; i += 1) {
    for (j = 0; j < strings.length; j += 1) {
        if (i === j) {
            continue;
        }
        pos = strings[i].indexOf(strings[j]);
        if (pos !== -1) {
            strings[i] = strings[i].substr(0, pos) + strings[i].substr(pos + strings[j].length);
        } 
    }
}

console.log(strings);
output: [
  "This is a string.",
  "here's another string. ",
  "And now there's a third string.  ",
  "Here's a string that might not follow the pattern.  "
];

Note that this code doesn't replace multiple occurances. If you want that, add a while-loop.

Solution 3:

Edit: This will cause problems if the string has special regex chars. A JS global repalce without regex can exploit split and join, (http://www.adequatelygood.com/JS-Find-and-Replace-with-SplitJoin.html) see below:

Javascript requires a regex to do a global replace, so something like this

var arr = [
    'This is a string.',
    "here's another string. This is a string.",
    "And now there's a third string.  here's another string. This is a string.",
    "Here's a string that might not follow the pattern.  This is a string."
];
for (var i = 0; i < arr.length; i++) {
    for (j = 0; j < arr.length; j++) {
        if (j !== i) {
            arr[i] = arr[i].split(arr[j]).join("");
        }
    }
}
console.log(arr);


[
"This is a string.", 
"here's another string.",
"And now there's a third string.",
"Here's a string that might not follow the pattern."
]

It should replace all occurences

Solution 4:

use like following. Use .map function of jquery to iterate over the array. the code given as following

var arr = [
           'This is a string.',
           "here's another string. This is a string.",
           "And now there's a third string.  here's another string. This is a string.",
           "Here's a string that might not follow the pattern.  This is a string."
         ];

     var out=$.map(arr,function (i,v){
         return i.substr(0,i.indexOf(".") + 1);
     });
     console.log("value is"+out);

Post a Comment for "Javascript/jquery Find And Delete Array Value In The Rest Of Array"