简体   繁体   English

javascript - 在每次地图迭代的开头添加对象

[英]javascript - add object at the beginning on each map iteration

So, let's suppose I have this:所以,让我们假设我有这个:

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

If I do this: var officersIds = officers.map(x => [x.name, x.lastName]);如果我这样做: var officersIds = officers.map(x => [x.name, x.lastName]); the result of officersIds is gonna be this:结果officersIds是会是这样的:

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

right?对?

So, what I need to do is to put an object on each map iteration so the output is now this, for example:所以,我需要做的是在每次地图迭代上放置一个对象,因此输出现在是这样的,例如:

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

Why do I need this?为什么我需要这个? Better don't ask ;) But it's a complex problem and if you help me solve this simple one I could transfer your solution to my complex problem.最好不要问;) 但这是一个复杂的问题,如果你帮我解决这个简单的问题,我可以将你的解决方案转移到我的复杂问题上。

IMPORTANT: Is there a way to do this in one line?重要提示:有没有办法在一行中做到这一点?

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

Or或者

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" ]);

Use Map over officers .用地图过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);

If you simply want to add ['x', 'y'] to the start of the array, you can use the spread operator to make it a one-liner:如果您只想将['x', 'y']到数组的开头,您可以使用扩展运算符使其成为单行:

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

See proof-of-concept below:请参阅下面的概念验证:

 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);


However, if you want to add ['x', 'y'] to each item in the array, then you should do this instead:但是,如果您想将['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);

You could just add the wanted parts to the mapping function.您可以将所需的部分添加到映射函数中。

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在.each迭代中返回对象-JavaScript - Returning an object in .each iteration - JavaScript 使用“unshift”方法将项目添加到 Map Object 的开头 - Add item to beginning of an Map Object with "unshift" method 在 Javascript 中使用 Map 每次迭代将每个值增加 5? - increase each value by 5 by each iteration using Map in Javascript? 如何在javascript中的对象开头添加属性 - How to add a property at the beginning of an object in javascript 如何在JavaScript中取消移位或将其添加到arguments对象的开头 - How to unshift or add to the beginning of arguments object in JavaScript 如何在div中每一行的开头添加特定的char? (javascript) - how to add a specific char at beginning of each line in div ? (javascript) Javascript 嵌套 map 迭代不更新我的对象的值 - Javascript Nested map iteration do not update my Object's Value Javascript:如何在每次通过迭代时添加最后一个数字? - Javascript: how to add the last number on each passing iteration? 如何在Javascript For循环中的每次迭代之间增加延迟 - How to add delay between each iteration in Javascript For loop 遍历JavaScript对象并在每次迭代中更改表内容 - Looping through JavaScript Object and changing table content on each iteration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM