2

I'm working with an angular project. I have a string like below and i sorted out all the special characters using regular expressions. Now, I want the first 2 strings to be in suare brackets and the second two in another square bracket like coordinates, and the output should be like below. Please help me achieve the functionality.

test: any = "((-1.23568 75.87956), (-1.75682 22.87694))"

My ts code be like:


  hello()
  {
      this.new = ( this.test.replace(/[^\d. -]/g, ''));
      this.newarr = this.new.split(" ");    
      const result =  this.newarr.filter(e =>  e);
  }

and my final output in result array is like:

["-1.23568", "75.87956", "-1.75682", "22.87694"]

Desired output

[ ["-1.23568", "75.87956"], ["-1.75682", "22.87694"] ]

3 Answers 3

1

Another approach would be to use String.match() to get each group of coordinates, then Array.map() and String.split() to divide into pairs.

let test = "((-1.23568 75.87956), (-1.75682 22.87694))";
const result = test.match(/[\d.-]+\s[\d.-]+/g).map(s => s.split(/\s/));
console.log('Result:', result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

2 Comments

Thanks for the response. As of now, we are putting every 2 strings into an array. But, what is the solution if we want to place every 2 strings in between the square brackets and pass the same into a variable ? Ex: test = [[string1, string2 ],[string1, string2], and so on ]. If I want to read index 0 - the value should be test = [string1, string2] and so on I can read any index and it should cone with square brackets.
I expect one could modify the solution in that case, if you add that variant to the question it would be helpful, thanks!
1

For example you can use Array.prototype.splice(). This function can give you such 'portions' for your new array:

pairs.push(initialArr.splice(0, 2))

Juts loop through your initial array initialArr.length/2 times.

Working demo

EDIT: Or maybe you can update your replacing logic for getting not just number after number but pairs (for example '-1.23568 75.87956,-1.75682 22.87694') and then just split it twice.

split(',') --> ['-1.23568 75.87956', '-1.75682 22.87694']

and then loop and

split(' ')

2 Comments

Thanks for the response. I didn't get your edit answer, can you please elaborate it.
@chlara check pls here
1

You can avoid some complex regex logic and just, once you have:

const [a1, a2, b1, b2] = ["-1.23568", "75.87956", "-1.75682", "22.87694"]

const result = [[a1, a2], [b1, b2]]

Or if there are more than two pairs:

const array = ["-1.23568", "75.87956", "-1.75682", "22.87694", "-1.33343", "3.34432"]

const result = [];
for(let i = 0; i < array.length; i+=2) {
  result.push(array.slice(i, i+2))
}

2 Comments

Thank you for the response. It worked.
All the three answers worked for me.

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.