简体   繁体   中英

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]); the result of officersIds is gonna be this:

[ "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 .

 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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM