0

I have the following array

0:{VERSION: 1, CURRENT_DATE: "1900-01-01", CURRENT_ACTIVITY_DATE: "2017-06-22"},
1:{VERSION: 2, PREVIOUS_DATE: "1900-01-01", PREVIOUS_ACTIVITY_DATE: "2017-06-22"}

I need to change the format of the date and update the array with the new formatted date. Any thoughts would be helpful.

the exact problem i am facing is array index is not the same in every row. The code works for "PREVIOUS_ACTIVITY_DATE" but for "CURRENT_ACTIVITY_DATE" the date format remains the same and is not changed. If i have more rows with different index i need to change the format for all rows

I am using moment.js for changing the format and that part is working fine.

Thanks in advance.

4
  • 3
    Iterate over the array and change each date individually? Commented Nov 6, 2017 at 8:18
  • 1
    I don't even see what your problem could be with the current use case, did you try? Commented Nov 6, 2017 at 8:18
  • How looks "new formatted date" ? Commented Nov 6, 2017 at 8:19
  • 1
    hm,, @LS2 I think that you are looking at wrong place for your issue, can you paste longer code, sort of what generates this array or more info.. Commented Nov 6, 2017 at 8:28

3 Answers 3

1

In case if you don't know what are the keys, you can access them by calling var keys = Object.keys(array[0]).

If you want to modify for ex. the current date, then simply you can do it by:

var currentDateKey = keys[1];
array[0][currentDateKey] = "newDateString";

and your array will be modified to the new value.

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

Comments

1

Well simply use Array#map() method over your array, it will update each item dates properties in the array:

arr.map(function(el) {
  if (el["PREVIOUS_ACTIVITY_DATE"])
    el["PREVIOUS_ACTIVITY_DATE"] = new Date(el["PREVIOUS_ACTIVITY_DATE"]);

  if (el["PREVIOUS_DATE"])
    el["PREVIOUS_DATE"] = new Date(el["PREVIOUS_DATE"]);

  if (el["CURRENT_ACTIVITY_DATE"])
    el["CURRENT_ACTIVITY_DATE"] = new Date(el["CURRENT_ACTIVITY_DATE"]);

  if (el["CURRENT_DATE"])
    el["CURRENT_DATE"] = new Date(el["CURRENT_DATE"]);

  return el;
});

Demo:

const arr = [{
    VERSION: 1,
    CURRENT_DATE: "1900-01-01",
    CURRENT_ACTIVITY_DATE: "2017-06-22"
  },
  {
    VERSION: 2,
    PREVIOUS_DATE: "1900-01-01",
    PREVIOUS_ACTIVITY_DATE: "2017-06-22"
  }
];

arr.map(function(el) {
  if (el["PREVIOUS_ACTIVITY_DATE"])
    el["PREVIOUS_ACTIVITY_DATE"] = new Date(el["PREVIOUS_ACTIVITY_DATE"]);

  if (el["PREVIOUS_DATE"])
    el["PREVIOUS_DATE"] = new Date(el["PREVIOUS_DATE"]);

  if (el["CURRENT_ACTIVITY_DATE"])
    el["CURRENT_ACTIVITY_DATE"] = new Date(el["CURRENT_ACTIVITY_DATE"]);

  if (el["CURRENT_DATE"])
    el["CURRENT_DATE"] = new Date(el["CURRENT_DATE"]);

  return el;
});

console.log(arr);

Note:

  • You need to check over the existence of the keys in the object.

  • I used new Date(el["PREVIOUS_ACTIVITY_DATE"]) as an example here, you just need to change it with your momentjs syantax.

3 Comments

Thanks for the reply. I guess i have not explained the question correctly. But the exact problem i am facing is array index is not the same in every row. The code works for "PREVIOUS_ACTIVITY_DATE" but for "CURRENT_ACTIVITY_DATE" the date format remains the same and is not changed. If i have more rows with different index i need to change the format for all rows.
@LS2 Please update your question with your code, anyway using Array.map() will work with all items even if they have the same index like you said.
@LS2 I think I understand what you mean, you need to check the existence of the keys in the object, before trying to change them. Please check my Edit.
0

You could go for some approach like:

var myArray = [
    {VERSION: 1, CURRENT_DATE: "1900-01-01", CURRENT_ACTIVITY_DATE: "2017-06-22"},
    {VERSION: 2, PREVIOUS_DATE: "1900-01-01", PREVIOUS_ACTIVITY_DATE: "2017-06-22"}
];

//Format all entries with the desired keys...
var formatKeys = ["CURRENT_DATE","CURRENT_ACTIVITY_DATE","PREVIOUS_DATE","PREVIOUS_ACTIVITY_DATE"];

// Iterate through your array
for(var i = 0; i < myArray.length; i++){
    // Iterate through the desired keys to format
    for(var key in formatKeys){
         if(myArray[i][formatKeys[key]] !== undefined)
         myArray[i][formatKeys[key]].format(); // Your format call
    }
}

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.