Tengo dos matrices, quiero crear una nueva matriz que coincida con array1.id == array2.toyId.
Array1 = [ { id: 2, name: 'arujn' }, { id: 3, name: 'abhinav' } ] Array2 = [ { id: 1, toyId: 2, label: 'label456', type: 'text56' }, { id: 2, toyId: 2, label: 'label22', type: 'text88' }, { id: 3, toyId: 3, label: 'labe ffum', type: 'text35' }, { id: 4, toyId: 3, label: 'label 8', type: 'text66' } ]Entonces, la nueva matriz tiene el nombre de la matriz1 y la etiqueta y el tipo de la matriz2,
Esa matriz se verá así: -
Estoy usando javascript para esto.
Array3 = [ { name: 'arjun', newdata: [ { label: 'label456', type: 'text56' }, { label: 'label22', type: 'text88' } ] }, { name: 'abhinav', newdata: [ { label: 'labe ffum', type: 'text35' }, { label: 'label 8', type: 'text66' } ] } ]Por favor ayúdenme a resolver esto, gracias de antemano.
const Array1 = [ {id: 2,name: 'arujn'}, {id: 3,name: 'abhinav'}], Array2 = [ {id: 1,toyId: 2,label: 'label456',type: 'text56'}, {id: 2,toyId: 2,label: 'label22',type: 'text88'}, {id: 3,toyId: 3,label: 'labe ffum',type: 'text35'}, {id: 4,toyId: 3,label: 'label 8',type: 'text66'}]; const Array3 = Array1.map(element => ({name: element.name, newdata: Array2.filter(obj => obj.toyId === element.id).map(({label, type}) => ({label, type}) )})) console.log(Array3);