【问题标题】:javascript - add object at the beginning on each map iterationjavascript - 在每次地图迭代的开头添加对象
【发布时间】:2020-01-07 09:55:49
【问题描述】:

所以,假设我有这个:

var officers = [
  { id: 20, name: 'Captain', lastName: 'Piett' },
  { id: 24, name: 'General', lastName: 'Veers'  },
  { id: 56, name: 'Admiral', lastName: 'Ozzel'  },
  { id: 88, name: 'Commander', lastName: 'Jerjerrod'  }
];

如果我这样做: var officersIds = officers.map(x => [x.name, x.lastName]); officersIds 的结果是这样的:

[ "Captain", "Piett" ], [ "General", "Veers" ], [ "Admiral", "Ozzel" ], [ "Commander", "Jerjerrod" ]

对吗?

所以,我需要做的是在每次地图迭代中放置一个对象,所以现在的输出是这样的,例如:

[["x", "y"], [ "Captain", "Piett" ]], 
[["x", "y"], [ "General", "Veers" ]],  
[["x", "y"], [ "Admiral", "Ozzel" ]], 
[["x", "y"], [ "Commander", "Jerjerrod" ]]

我为什么需要这个?最好不要问;)但这是一个复杂的问题,如果你帮我解决这个简单的问题,我可以将你的解决方案转移到我的复杂问题中。

重要提示:有没有办法在一行中做到这一点?

【问题讨论】:

  • 您的意思是您需要在officersIds 的开头添加一个新元素["x", "y"]
  • 您的示例看起来与您已经做的几乎相同。你提到了物体,但我没有看到?期望的输出到底是什么?
  • officers.map(x => [["x", "y"], [x.name, x.lastName]])。只需从地图回调中返回一个二维数组。
  • 对不起,我更正了输出
  • 好吧,这就是诀窍,我能找到答案来标记它已解决吗?

标签: javascript arrays dictionary object add


【解决方案1】:

如果您只是想将['x', 'y'] 添加到数组的开头,可以使用spread operator 使其成为单行:

var officersIds = [['x', 'y'], ...officers.map(x => [x.name, x.lastName])];

请参阅下面的概念验证:

var officers = [
  { id: 20, name: 'Captain', lastName: 'Piett' },
  { id: 24, name: 'General', lastName: 'Veers'  },
  { id: 56, name: 'Admiral', lastName: 'Ozzel'  },
  { id: 88, name: 'Commander', lastName: 'Jerjerrod'  }
];

var officersIds = [['x', 'y'], ...officers.map(x => [x.name, x.lastName])];

console.log(officersIds);

但是,如果您想将 ['x', 'y'] 添加到数组中的每个项目,那么您应该这样做:

var officersIds = officers.map(x => [['x', 'y'], [x.name, x.lastName]]);

var officers = [
  { id: 20, name: 'Captain', lastName: 'Piett' },
  { id: 24, name: 'General', lastName: 'Veers'  },
  { id: 56, name: 'Admiral', lastName: 'Ozzel'  },
  { id: 88, name: 'Commander', lastName: 'Jerjerrod'  }
];

var officersIds = officers.map(x => [['x', 'y'], [x.name, x.lastName]]);

console.log(officersIds);

【讨论】:

    【解决方案2】:
    var officers = [
      { id: 20, name: 'Captain', lastName: 'Piett' },
      { id: 24, name: 'General', lastName: 'Veers'  },
      { id: 56, name: 'Admiral', lastName: 'Ozzel'  },
      { id: 88, name: 'Commander', lastName: 'Jerjerrod' },
    ];
    
    officers.unshift({ name: x, lastName: y });
    
    var officersIds = officers.map(x => [x.name, x.lastName]);
    

    或者

    var officers = [
          { id: 20, name: 'Captain', lastName: 'Piett' },
          { id: 24, name: 'General', lastName: 'Veers'  },
          { id: 56, name: 'Admiral', lastName: 'Ozzel'  },
          { id: 88, name: 'Commander', lastName: 'Jerjerrod' },
        ];
    
    
        var officersIds = officers.map(x => [x.name, x.lastName]);
    
        officersIds.unshift(["x", "y" ]);
    

    【讨论】:

    • 可能缺少一些引号
    【解决方案3】:

    您可以将想要的部分添加到映射函数中。

    var officers = [{ id: 20, name: 'Captain', lastName: 'Piett' }, { id: 24, name: 'General', lastName: 'Veers'  }, { id: 56, name: 'Admiral', lastName: 'Ozzel'  }, { id: 88, name: 'Commander', lastName: 'Jerjerrod'  }],
        officersIds = officers.map(x => [["x", "y"], [x.name, x.lastName]]);
    
    console.log(officersIds);

    【讨论】:

      【解决方案4】:

      officers 上使用地图。

      var officers = [
        { id: 20, name: 'Captain', lastName: 'Piett' },
        { id: 24, name: 'General', lastName: 'Veers'  },
        { id: 56, name: 'Admiral', lastName: 'Ozzel'  },
        { id: 88, name: 'Commander', lastName: 'Jerjerrod'  }
      ];
      
      var officersIds = officers.map(x => [["x", "y"], [x.name, x.lastName]]);
         
      console.log(officersIds);

      【讨论】:

      • .splice(0, 0, x) 可以写成.unshift(x)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 2013-10-27
      • 2017-10-25
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      相关资源
      最近更新 更多