0

I am trying to update nested array elements in javascript. want to convert dates into different format. how i can updates nested elements

array1 = [
{
"week": [
"2019-05-06T16:00:00.000Z",
"2019-05-07T16:00:00.000Z",
"2019-05-08T16:00:00.000Z",
"2019-05-09T16:00:00.000Z",
"2019-05-10T16:00:00.000Z",
"2019-05-11T16:00:00.000Z",
"2019-05-12T16:00:00.000Z"
],
"weekNumber": 19
},
{
"week": [
"2019-05-20T16:00:00.000Z",
"2019-05-21T16:00:00.000Z",
"2019-05-22T16:00:00.000Z",
"2019-05-23T16:00:00.000Z",
"2019-05-24T16:00:00.000Z",
"2019-05-25T16:00:00.000Z",
"2019-05-26T16:00:00.000Z"
],
"weekNumber": 21
},
{
"week": [
"2019-06-03T16:00:00.000Z",
"2019-06-04T16:00:00.000Z",
"2019-06-05T16:00:00.000Z",
"2019-06-06T16:00:00.000Z",
"2019-06-07T16:00:00.000Z",
"2019-06-08T16:00:00.000Z",
"2019-06-09T16:00:00.000Z"
],
"weekNumber": 23
}
];

expectedResult = [
{
"week": [
"2019-05-06",
"2019-05-07",
"2019-05-08",
"2019-05-09",
"2019-05-10",
"2019-05-11",
"2019-05-12"
],
"weekNumber": 19
},
{
"week": [
"2019-05-20",
"2019-05-21",
"2019-05-22",
"2019-05-23",
"2019-05-24",
"2019-05-25",
"2019-05-26"
],
"weekNumber": 21
},
{
"week": [
"2019-06-03",
"2019-06-04",
"2019-06-05",
"2019-06-06",
"2019-06-07",
"2019-06-08",
"2019-06-09"
],
"weekNumber": 23
}
];

want to remove ":00:00.000Z" . I have format function which removed that but do not know how to call here

want to remove ":00:00.000Z" . I have format function which removed that but do not know how to call here

3 Answers 3

1

You can use map and split

let array1 = [{"week": ["2019-05-06T16:00:00.000Z","2019-05-07T16:00:00.000Z","2019-05-08T16:00:00.000Z","2019-05-09T16:00:00.000Z","2019-05-10T16:00:00.000Z","2019-05-11T16:00:00.000Z","2019-05-12T16:00:00.000Z"],"weekNumber": 19},
{"week": ["2019-05-20T16:00:00.000Z","2019-05-21T16:00:00.000Z","2019-05-22T16:00:00.000Z","2019-05-23T16:00:00.000Z","2019-05-24T16:00:00.000Z","2019-05-25T16:00:00.000Z","2019-05-26T16:00:00.000Z"],"weekNumber": 21},
{"week": ["2019-06-03T16:00:00.000Z","2019-06-04T16:00:00.000Z","2019-06-05T16:00:00.000Z","2019-06-06T16:00:00.000Z","2019-06-07T16:00:00.000Z","2019-06-08T16:00:00.000Z","2019-06-09T16:00:00.000Z"],"weekNumber": 23}];

let op = array1.map(e=>{
  e.week = e.week.map(val => val.split('T',1)[0])
  return e
})

console.log(op)

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

Comments

1

You can use map if you want a new array of can use map inside a forEach. Basically you need to use nested map.

This example shows use of two map, one nested inside another. In the nested map , the week is iterated and here the Date object is use. If you have a separate function you can call it inside nested map call back function like this

 let newfmt = array1.map(function(item) {
      return {
        week: item.week.map(function(elem) {
          // here elem will be each value inside the week array.
          // since map create an array here week will be an array of formatted dates
          return yourFunction(elem)
        }),
        weekNumber: item.weekNumber
      }
    })

let array1 = [{
    "week": [
      "2019-05-06T16:00:00.000Z",
      "2019-05-07T16:00:00.000Z",
      "2019-05-08T16:00:00.000Z",
      "2019-05-09T16:00:00.000Z",
      "2019-05-10T16:00:00.000Z",
      "2019-05-11T16:00:00.000Z",
      "2019-05-12T16:00:00.000Z"
    ],
    "weekNumber": 19
  },
  {
    "week": [
      "2019-05-20T16:00:00.000Z",
      "2019-05-21T16:00:00.000Z",
      "2019-05-22T16:00:00.000Z",
      "2019-05-23T16:00:00.000Z",
      "2019-05-24T16:00:00.000Z",
      "2019-05-25T16:00:00.000Z",
      "2019-05-26T16:00:00.000Z"
    ],
    "weekNumber": 21
  },
  {
    "week": [
      "2019-06-03T16:00:00.000Z",
      "2019-06-04T16:00:00.000Z",
      "2019-06-05T16:00:00.000Z",
      "2019-06-06T16:00:00.000Z",
      "2019-06-07T16:00:00.000Z",
      "2019-06-08T16:00:00.000Z",
      "2019-06-09T16:00:00.000Z"
    ],
    "weekNumber": 23
  }
];

let newfmt = array1.map(function(item) {
  return {
    week: item.week.map(function(elem) {
      let dt = new Date(elem);
      return `${dt.getFullYear()}-${dt.getMonth()}-${dt.getDay()}`
    }),
    weekNumber: item.weekNumber
  }
})
   console.log(newfmt)

Comments

0

You can use Object.assign for example:-

const a = { x: "Hi", y: "Test" } const b = Object.assign({}, a, { x: "Bye" }); console.log(b);

Here rather then x: "Bye" you can iterate on week and do the formatting.

1 Comment

Yes, using map is a much better way.

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.