如何在javascript中更新子数组元素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在javascript中更新子数组元素相关的知识,希望对你有一定的参考价值。

我试图在javascript中更新嵌套的数组元素。想要将日期转换为不同的格式。我如何更新嵌套元素

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
}
];

想要删除“:00:00.000Z”。我有格式化功能删除了但不知道如何在这里调用

想要删除“:00:00.000Z”。我有格式化功能删除了但不知道如何在这里调用

答案

你可以使用mapsplit

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)
另一答案

如果你想要一个新的数组可以在map中使用map,你可以使用forEach。基本上你需要使用嵌套的地图。

这个例子显示了两个map的使用,一个嵌套在另一个中。在嵌套映射中,迭代week,这里使用Date对象。如果你有一个单独的函数,你可以在嵌套的map回调函数中调用它

 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)
另一答案

您可以使用Object.assign例如: -

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

在这里,而不是x: "Bye"你可以在一周迭代并进行格式化。

以上是关于如何在javascript中更新子数组元素的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript单行代码,也就是代码片段

如何使用猫鼬更新子文档数组中的特定元素? [复制]

为什么我不能在此片段中生成唯一对象数组?

JavaScript中如何通过一个元素去查找该元素的子元素节点

如何将此 JavaScript 代码片段翻译成 Parenscript?

JavaScript笔试题(js高级代码片段)