【问题标题】:Create new array with two columns from old array使用旧数组中的两列创建新数组
【发布时间】:2019-10-07 14:19:06
【问题描述】:

我有一个数组如下:

items = [
{"year": 2010, "month": 11, "day":23}
{"year": 2009, "month": 10, "day":15}
{"year": 2009, "month": 10, "day":10} //added after my edit below
]

我想创建一个只有 2 列的新数组,如下所示:

newArarry = [
{"year": 2010, "month": 11}
{"year": 2009, "month": 10}
]

现在我正在尝试使用 .map() 并且它不起作用:

const newArray = [];
newArray.map({
    year: items.year,
    month: items.month
});

编辑

在遵循以下答案之一后,我意识到我忘了提及我还需要将结果过滤为唯一的行。现在我只选择年份和月份列,我得到了多个重复的行

【问题讨论】:

  • 在 Items 上做地图,它会返回给你新的数组。
  • @MennyMez 查看我的更新答案以说明您的编辑。

标签: javascript arrays object ecmascript-6


【解决方案1】:

现在我正在尝试使用 .map() 并且它不起作用

  • 因为 newArray 的长度为 0,而您正尝试在其上进行映射
  • map 接受回调,但您正在传递对象
  • 不将映射的输出分配给任何变量

let items = [{"year": 2010, "month": 11, "day":23},{"year": 2009, "month": 10, "day":15}]

let final = items.map(({day,...rest}) => ({...rest}))

console.log(final)

【讨论】:

    【解决方案2】:

    Array 的.map() 方法接受一个回调,其返回值将用于构造新数组。您错误地传递给它的是一个对象而不是一个函数。要解决此问题,您可以使用 .map() 和一些对象解构:

    const items = [
      {"year": 2010, "month": 11, "day":23},
      {"year": 2009, "month": 10, "day":15}
    ];
    
    const result = items.map(({ year, month }) => ({year, month}));
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案3】:

      你没有正确调用Array#map

      1. 您需要调用 source 数组:items
      2. 您需要提供一个函数作为参数。
      3. Array#map 返回一个新数组,该数组是针对原始数组的每个成员执行的函数的结果。

      items = [
      {"year": 2010, "month": 11, "day":23},
      {"year": 2009, "month": 10, "day":15}
      ]
      
      const newArray = items.map(item => ({
          year: item.year,
          month: item.month
      }));
      
      console.log(newArray)

      A mapping 操作从 A 转换为 B - 在本例中从 {"year": 2010, "month": 11, "day":23} 转换为 { "year": 2010, "month": 11 }。在数组上运行 .map 可以得到映射操作的结果。

      【讨论】:

      • 谢谢,我目前正在使用这种方法。但是,我现在得到重复的行。有没有办法只选择唯一的行(我的项目数组比我的问题大得多)
      • @Mennyg 看看at this answer
      【解决方案4】:

      Array.map() 需要回调作为第一个参数,它也必须在原始数组上调用 - 否则您实际上是在迭代一个空数组。

      var newArray = items.map(function (item) {
          return {
              year: item.year,
              month: item.month
          };
      });
      

      更新

      为了确保您的数组只包含唯一值,而不是映射和过滤,您可以执行类似于以下的操作:

      var items = [
          {year: 2010, month: 11, day: 23},
          {year: 2009, month: 10, day: 15},
          {year: 2009, month: 10, day: 10}
      ];
      
      var newArray = [];
      
      items.forEach(function (item) {
          var index = newArray.findIndex(function (newItem) {
              return newItem.year === item.year && newItem.month === item.month;
          });
      
          if (index === -1) {
              newArray.push({ year: item.year, month: item.month });
          }
      });
      
      console.log(newArray); // [{ year: 2010, month: 11 }, { year: 2009, month: 10 }]
      

      【讨论】:

        【解决方案5】:

        您没有正确使用map - 您还可以通过休息、传播和解构来简化:

        const items = [{"year": 2010, "month": 11, "day":23},{"year": 2009, "month": 10, "day":15}];
        const newArray = items.map(({ day, ...res }) => ({ ...res }));
        console.log(newArray);
        .as-console-wrapper { max-height: 100% !important; top: auto; }

        【讨论】:

          【解决方案6】:

          更正您的项目数组,具有多个元素/值的数组必须用逗号分隔

          var items = [{"year": 2010, "month": 11, "day":23},
                         {"year": 2009, "month": 10, "day":15}]
                              const newArray = [];
                              items.map(i=>{
                              newArray.push({year: i.year, month: i.month});
                              });
                            console.log(newArray);

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-12
            • 1970-01-01
            • 2021-12-14
            • 1970-01-01
            • 2016-02-04
            • 2022-07-07
            相关资源
            最近更新 更多