Last active
May 24, 2024 13:03
-
-
Save waicampos/ffe6dd11c81a69364625c2af4919f6d5 to your computer and use it in GitHub Desktop.
Função para concatenar objetos JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var demand_cost = [ | |
{ peak_demand: 500, off_peak_demand: 800 }, | |
{ peak_demand: 1500, off_peak_demand: 1800 }, | |
]; | |
var energy_cost = [ | |
{ peak_energy: 600, off_peak_energy: 900 }, | |
{ peak_energy: 1600, off_peak_energy: 1900 }, | |
]; | |
function concat_obj(...arrays_obj) { | |
let new_arr_of_obj = Array.from({ length: arrays_obj[0].length }, () => ({})); | |
[...Array(arrays_obj.length).keys()].forEach((array_index) => { | |
arrays_obj[array_index].forEach((el, index_el) => { | |
Object.keys(el).forEach((key) => { | |
new_arr_of_obj[index_el][key] = el[key]; | |
}); | |
}); | |
}); | |
return new_arr_of_obj; | |
} | |
const costs = concat_obj(demand_cost, energy_cost); | |
console.log(demand_cost, energy_cost); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment