Skip to content

Instantly share code, notes, and snippets.

@enescang
Last active March 27, 2023 23:46
Show Gist options
  • Save enescang/781d3826387a569a2c28154f3c68e9d5 to your computer and use it in GitHub Desktop.
Save enescang/781d3826387a569a2c28154f3c68e9d5 to your computer and use it in GitHub Desktop.
currency_replacer.js
const json = [
{
"price":"1NCH 23,33"
},
{
"price":"# 3443,01"
},
{
"price":"109,10000 kr"
},
{
"price":"Q 232,3#"
},
{
"price":"@something66,98#"
},
{
"price": "100"
}
]
for(let i=0; i<json.length; i++){
const price = json[i].price;
let left_pointer = -1;
let right_pointer = -1;
for(let k=0; k<price.length; k++){
const c = price[k];
if(c == ",")
continue;
const is_number = isNaN(c) == false;
if(is_number == false){
if(left_pointer == right_pointer)
left_pointer = -1;
continue;
}
if(is_number){
if(left_pointer == -1)
left_pointer = k;
right_pointer = k;
}
}
const flag_result = price.substring(left_pointer, right_pointer+1);
let num_part = flag_result.replace(",",".");
num_part = parseFloat(num_part) * 2;
json[i].price = price.replace(flag_result.replace(/\s/g,""), num_part);
}
console.log(json);
/*
Sample Output:
[
{ price: '1NCH 46.66' },
{ price: '# 6886.02' },
{ price: '218.2 kr' },
{ price: 'Q 464.6#' },
{ price: '@something133.96#' },
{ price: '200' }
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment