javascript - filter array of strings based on seperated ending

Viewed 203

I have an array of strings like this:

const strings = [
  "author:app:1.0.0",
  "author:app:1.0.1",
  "author:app2:1.0.0",
  "author:app2:1.0.2",
  "author:app3:1.0.1"
];

And I want to filter them so that only the ones that have the latest versions for the given "author:name" are left, thus removing ones that are not the latest (i.e. the "1.0.1" ones).

My expected result is this:

const filteredStrings = [
  "author:app:1.0.1",
  "author:app2:1.0.2",
  "author:app3:1.0.1"
];

Any way to do this simply?

4 Answers

You can do it with two loops first one find new ones second one check which is bigger

const strings = [
  "author:app:1.0.0",
  "author:app:1.0.1",
  "author:app2:1.0.0",
  "author:app2:1.0.2",
  "author:app3:1.0.1"
];
filteredones = [];
strings.forEach(element => {
  var arr = element.split(":");
  var isnew = true;
  var found = filteredones.find(function(element2) {
    var x = element2.split(":");
    return x[1] == arr[1] && x[0] == arr[0]
  });
  if (found == undefined) {
    filteredones.push(element);
  }
});
for (var i = 0; i < filteredones.length; i++) {
  element = filteredones[i];
  var arr = element.split(":");
  var isnew = true;
  var found = strings.find(function(element2) {
    var x = element2.split(":");
    return x[1] == arr[1] && x[0] == arr[0] && x[2] > arr[2]
  });
  if (found != undefined) {
    filteredones[i] = found;
  }
};

console.log(filteredones);

you can check the value in the last index of the string in each of the elements of the array and if they qualify as a latest one put it to a new array.

Build an object with keys as app name.
getValue method is calculate the version value so that to compare.
Update object value, when you see the version is recent (value is big).

const strings = [
  "author:app:1.0.0",
  "author:app:1.0.1",
  "author:app2:1.0.0",
  "author:app2:1.0.2",
  "author:app3:1.0.1"
];

const filter = data => {
  const res = {};
  const getValue = item =>
    item
      .split(":")[2]
      .split(".")
      .reduceRight((acc, curr, i) => acc + curr * Math.pow(10, i), 0);
  data.forEach(item => {
    const app = item
      .split(":")
      .slice(0, 2)
      .join(":");
    if (!res[app] || (app in res && getValue(item) > getValue(res[app]))) {
      res[app] = item;
    }
  });
  return Object.values(res);
};

console.log(filter(strings));

You can use an object to store the key/version pairs, and convert to appropriate output on the end. The version comparison can be any of those found here: How to compare software version number using js? (only number)

result = {};
for (var s of input) {
  // parts = ["author", "appname", "version"]
  var parts = s.split(":");
  var i = parts[0] + ":" + parts[1];
  if (!result[i] || compareVersion(parts[2], result[i]))
    // If not present or version is greater
    result[i] = parts[2]; // Add to result
}
result = Object.keys(result).map(k => k + ":" + result[k])

Working demo: https://codepen.io/bortao/pen/LYVmagK

Related