0

I prefer an underscore solution for this if possible but I'll take a vanilla JS solution if that is the best I can get.

I want to use array 2 to edit strings in the objects of array1 that start with or end with the strings in array2. The result I'm looking for can be seen in the results of array3 below:

array1 = [{key1: "Patty Bakery", key2: stuff}, {key1: "Bob Wine Shack",
key2: mattersNot}, {key1: "Romeo Clothing", key2: things}, {key1:
"StackIt", key2: finished}];

array2 = ["Patty", "Romeo", "Wine Shack"];

array3 = [{key1: "Bakery", key2: stuff}, {key1: "Bob",
key2: mattersNot}, {key1: "Clothing", key2: things}, {key1:
"StackIt", key2: finished}];

So far the best I've been able to do is remove the whole object in array1 with this code:

array1.filter(function(a){
return!_.some(array2, function(b){
return startsWith(a.key1, b)})})
//I have installed and am using underscore.string

which gives me an array3 that looks like

array3 = [{key1:"StackIt", key2: finished}];
4
  • 1
    are the strings that you want to change always under the key key1 or can they differ? Commented Jan 21, 2016 at 12:10
  • @MartinSchneider good question - they are always under key1. Commented Jan 21, 2016 at 12:28
  • 1
    What if string would be "Patty Whine Shack Romeo"? Should script quit after stripping just Patty, or do it until there are no further terms to be stripped and thus return empty string? Commented Jan 21, 2016 at 12:33
  • @MartinSchneider, thanks for being persistent. I had gone to sleep after I received the solution and just saw your response. The use case you mention would not happen in my environment. Upon waking I realized that I should be able to edit something like "Something Patty Romeo SomethingElse" to be "Something SomethingElse". Any thoughts on that? Commented Jan 22, 2016 at 0:41

1 Answer 1

1

You can use Regex for pattern starting with or ending with.

Following is a snippet depicting the same

var array1 = [{
  key1: "Patty Bakery",
  key2: "stuff"
}, {
  key1: "Bob Wine Shack",
  key2: "mattersNot"
}, {
  key1: "Romeo Clothing",
  key2: "things"
}, {
  key1: "StackIt",
  key2: "finished"
}];

var array2 = ["Patty", "Romeo", "Wine Shack"];
var array3 = [];
array2.forEach(function(item) {
  
  var startWithReg = new RegExp("^" + item);
  var endsWithReg = new RegExp(item + "$");
  
  console.log(startWithReg)
  
  array3 = array1.map(function(row) {
    
    row.key1 = row.key1.replace(startWithReg, '').trim();
    row.key1 = row.key1.replace(endsWithReg, '').trim();
    return row;
    
  });
})

console.log(array3)

Sign up to request clarification or add additional context in comments.

1 Comment

Nice solution but I end up with an extra space on either the front or the back. Could you help me clean that up?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.