2

My list is this: [ '030', '040', '050', '060', '070', '080', '090', '100', '110' ]

I wanted to know if it's possible to replace all zero's with nothing except the one in '100', in there i want just to replace the last one so it would be '10'.

let sizeCodes = body['items'][0]['product']['possibilities']['size']
let codeList = []
sizeCodes.forEach(sizeCode => {
    sizeC = sizeCode['code']
    codeList.push(sizeC)
})
codeList.forEach(code => {
    let variants = body['items'][0]['product']['variantOptions']['variants'][`${code}`]['stockLevel']['stockLevel']
    log('Size: ' + code.replace(/0/g, '') + '    |    Stock: ' + variants, 'info')
})

This is what i tried but obviously that replaces all zero's. Also i don't want to do that in the actual array, the '030' stands for size 3, '040' for size 4, etc. i just want to make it look cleaner by outputing that instead.

2
  • 2
    Parse them to integers and divide by ten? Commented Jul 17, 2019 at 18:15
  • arr.map(n => n / 10) Commented Jul 17, 2019 at 18:16

2 Answers 2

4

Sounds like you want to strip any leading zeros and divide all the values by ten.

If so:

const codes =  [ '030', '040', '050', '060', '070', '080', '090', '100', '110' ];

const adjustedCodes = codes.map(n => (n / 10).toString());

console.log(adjustedCodes);

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

Comments

0

Improvemement of JLRishe answer, probably the shortest way of do it

codes.map(x=>''+x/10);

let codes = [ '030', '040', '050', '060', '070', '080', '090', '100', '110' ]

let r = codes.map(x=>''+x/10);

console.log(r);

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.