0

I have an array:

result = [
  "205/65 15", 
  "205/65 16", 
  "205/65 17.5"
];

And I would like to extract the same string for items in the array, in this case 205/65 1.

And it should leave the array as:

extracted = "205/65 1";

leftover = [
  "5", 
  "6", 
  "7.5"
];

extracted is a variable the needs to be populated with the duplicate string that was extracted. It is not known before hand.

4
  • 1
    What if there are multiple "same strings"? Does it have to start with the "same string" or can it be in the middle? Commented Feb 8, 2021 at 9:47
  • @ritaj Has to start with the same string. Meanwhile we don't know the duplicate value that needs to be extracted. Commented Feb 8, 2021 at 9:48
  • 1
    Do you want to get the longest common substring from the beginning? Commented Feb 8, 2021 at 9:53
  • @adiga Correct. Commented Feb 8, 2021 at 9:54

3 Answers 3

3

You can do it by using map through the array, then filter out the matching part of the string contents. And then replace the string values:

const result = [
  "205/65 15",
  "205/65 16",
  "205/65 17.5"
];

let extracted = '';
const leftover = result.map((singleStr, index, arr) => {
  extracted = singleStr.split('').filter((singleChar, i) => {
    return arr[index + 1] ? singleChar === arr[index + 1].charAt(i) : '';
  }).join('') || extracted;

  return singleStr.replace(extracted, '');
});

console.log(leftover);

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

1 Comment

I don't know the extracted variable before hand. Thats a variable that needs to be populated with the duplicate string.
1

If the extracted value is always at the beginning of the string, you can easily write a function that iterates over the array and checks for overlaps:

function extractBeginning(arr){
  if(!arr.length)
    return {
      extracted: '', 
      leftover: []
    }
  let extractable = arr[0]
  for(const elem of arr.slice(1)){
    let i = 0
    while(i < extractable.length && extractable[i] === elem[i])
      i++
    extractable = extractable.slice(0, i)
  }
  return {
    extracted: extractable,
    leftover: arr.map(elem => elem.slice(extractable.length))
  }
}

const input = [
  "205/65 15", 
  "205/65 16",
  "205/65 17.5"
];

console.log(extractBeginning(input))

Comments

-1

It is very, you can use jQuery.each() function for extracting the array and remove the string by use JavaScript String replace() method from the array value.

Here is an example:

var arr1 = [];
var arr = ["205/65 15", "205/65 16", "205/65 17.5"];

jQuery.each( arr, function( i, val ) {
  alert(val);
  var res = val.replace("205/65 1", "");
  arr1.push(res);
});

console.log(arr1);

var arr1 = [];
var arr = ["205/65 15", "205/65 16", "205/65 17.5"];
jQuery.each( arr, function( i, val ) {
  var res = val.replace("205/65 1", "");
  arr1.push(res);
});
console.log(arr1);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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.