I have a code snippet which performs an SQL-style join on objects in two arrays based on a specific field (in the code below, .id
).
let preprocessed_array = [
{id: 1,category:'a'},
{id: 2,category:'b'},
{id: 3,category:'b'}
]
let new_array = [
{id: 4},
{id: 1,category:'b'},
{id: 5}
]
for(let i=0;i< preprocessed_array.length;i++)
{
for(let j=0;j<new_array.length;j++)
{
if( preprocessed_array[i].id===new_array[j].id)
{
new_array[j]=preprocessed_array[i]
}
}
}
console.log(new_array)
Although this snippet is straightforward, I don't like the imperative style. Is there a more declarative way to do this just with javascript / ECMA6 arrays?