It could be done like this:
var result = data.reduce (
(acc, obj) => Object.assign(acc, { [idxMapping[obj.id]]: obj }),
Object.keys(idxMapping).map( _ => null) );
var idxMapping = {
abc: 1,
foo: 2,
bar: 0
};
var data = [{
id: 'abc',
info1: 'random info 1',
info2: 'random info 2',
}, {
id: 'foo',
info1: 'random info 1',
info2: 'random info 2',
}];
var result = data.reduce (
(acc, obj) => Object.assign(acc, { [idxMapping[obj.id]]: obj }),
Object.keys(idxMapping).map( _ => null) );
console.log(result);
This assumes that the indices mentioned in idxMapping are in the range 0..n-1, where n is the number of elements in that array.
If it were OK to have undefined instead of null in the result array, then the Object.keys() part could be replaced by just [].
Explanation
First an array is created with as many null entries as there are keys in idxMapping:
Object.keys(idxMapping).map( _ => null)
This becomes the initial accumulator value for reduce, which then iterates over each element of data. In each iteration the corresponding index is retrieved from idxMapping:
idxMapping[obj.id]
With the ES6 syntax for computed property names, this value (a number) is used as a key of the (accumulator) array object (which really is an index of the array, but indexes of an array are essentially object properties):
[idxMapping[obj.id]]
... and the data element is assigned to it within an object literal:
{ [idxMapping[obj.id]]: obj }
For example, the above may resolve to this:
{ '1': obj }
...and further:
{ '1': {
id: 'abc',
info1: 'random info 1',
info2: 'random info 2',
} }
This object is then merged with the already existing accumulator object, using Object.assign:
Object.assign(acc, { [idxMapping[obj.id]]: obj })
... which comes down to setting one of the entries of the (accumulating) result array to the correct value. Continuing the above example, you actually get this assignment:
acc[1] = {
id: 'abc',
info1: 'random info 1',
info2: 'random info 2',
}
This array is also the return value of Object.assign, which is nice, as reduce expects the inner callback function to return the new value of the accumulator, and passes it in the next iteration back to the callback function.
At the last iteration this return value becomes the return value of reduce itself, and thus the value of result.