3

I am trying to use Javascript to remove the dash and everything after in an array of product ids. But not if the dash is in the middle of the product id. For example product ids:  

["630932-8", "640993-8", "1951-83044226", "641452-10", "1924-57044108", "630695-8 DD/F", "641058-8", "630932-20", "630695-14 DD/F", "630695-22 D/DD", "630695-12 C/D", "661049-6", "661050-4", "630692-8 C/D", "630513-38 G", "630513-36 F", "630655-36 DD", "630695-8 C/D", "661049-4", "630695-16 C/D"]

This is for use within a tag management system and need the to drop everything after the "-" so it would be

["630932", "640993", "1951-83044226", "641452", "1924-57044108", "630695", "641058", "630932", "630695", "630695", "630695", "661049", "661050", "630692", "630513", "630513", "630655", "630695", "661049", "630695"]

There will be some that have will have 4 digits then the dash and those need to stay. The Product ids are currently populated in a datalayer with the "-" they are referenced by utag.data.product_skus

4 Answers 4

1

You can use map() and split().

  • Use map() on the array.
  • split() all the strings by -.
  • Check if first part of result of split() has length 4 then return the original string.
  • Otherwise return the first part of the result obtained from split()

let arr = ["630932-8", "640993-8", "1951-83044226", "641452-10", "1924-57044108", "630695-8 DD/F", "641058-8", "630932-20", "630695-14 DD/F", "630695-22 D/DD", "630695-12 C/D", "661049-6", "661050-4", "630692-8 C/D", "630513-38 G", "630513-36 F", "630655-36 DD", "630695-8 C/D", "661049-4", "630695-16 C/D"]


let res = arr.map(x => {
  let parts = x.split('-');
  return parts[0].length === 4 ? x : parts[0];
  
})

console.log(res)

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

Comments

1

You could replace unwanted parts.

function remove(s) {
    return s.replace(/^(\d{5,})-.*$/, '$1');
}

var array = ["630932-8", "640993-8", "1951-83044226", "641452-10", "1924-57044108", "630695-8 DD/F", "641058-8", "630932-20", "630695-14 DD/F", "630695-22 D/DD", "630695-12 C/D", "661049-6", "661050-4", "630692-8 C/D", "630513-38 G", "630513-36 F", "630655-36 DD", "630695-8 C/D", "661049-4", "630695-16 C/D"];

console.log(array.map(remove));

Comments

0

Use map and split, with a regex to check the 4-digit case:

const data = ["630932-8", "640993-8", "1951-83044226", "641452-10", "1924-57044108", "630695-8 DD/F", "641058-8", "630932-20", "630695-14 DD/F", "630695-22 D/DD", "630695-12 C/D", "661049-6", "661050-4", "630692-8 C/D", "630513-38 G", "630513-36 F", "630655-36 DD", "630695-8 C/D", "661049-4", "630695-16 C/D"];
const res = data.map(s => /^\d{4}-/.test(s) ? s : s.split("-")[0]);
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

Comments

0

You could proceed like this

var data = ["630932-8", "640993-8", "1951-83044226", "641452-10", "1924-57044108", "630695-8 DD/F", "641058-8", "630932-20", "630695-14 DD/F", "630695-22 D/DD", "630695-12 C/D", "661049-6", "661050-4", "630692-8 C/D", "630513-38 G", "630513-36 F", "630655-36 DD", "630695-8 C/D", "661049-4", "630695-16 C/D"];

var needed_data = [];
data.map(function(val) {
  var matches = val.match(/^([0-9]{5,})-.*/);
  if(matches) {
    needed_data.push(matches[1])
  } else  {
    var submatches = val.match(/(^[0-9]{4})-([0-9]*)/);
    if(submatches[1] && submatches[2]) {
      needed_data.push(val)
    }
  }
});

console.log(needed_data);

Comments

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.