Home > Software engineering >  Change the order of an object in an array
Change the order of an object in an array

Time:10-25

I'm working with an XML data that gives all information for my vue app. Everything works fine except one value.

I need to get the date from an element, but it comes in this format :

['2022-10-25']

It should be 25-10-2022

I get this item using :

item.datum.join().toString()

How can I change it ?

Since it's an Object, I can not find a solution for this, better would be to handle it as data, I have MomentJS installed but I also did not get it to work.

CodePudding user response:

An easy way to to this is split the string by the character "-" to an array which we reverse and join it togheter with "-".

// result step 1
console.log('2022-10-25'.split("-"))
// result step 2
console.log('2022-10-25'.split("-").reverse())
// final result
console.log('2022-10-25'.split("-").reverse().join("-"))

CodePudding user response:

Can't you simply reverse the string?

const dates = ['2022-10-25']
const desiredDate = dates[0].split('-').reverse().join('-')

  • Related