A continuación se muestra el Array de referenceArray para comparar y manipular el Array originalArray .
const referenceArray = [ { name: 'animal', source: ['duck', 'cat'], target: ['water', 'ground'] }, { name: 'car', source: ['tata', 'kia'], target: ['tiago', 'sector'] }, { name: 'bike', source: ['honda', 'hero'], target: ['livo', 'xtream'] }, { name: 'vehicle', source: ['honda', 'hero'], target: ['hard', 'soft'] }, ]; Y esto es: quiero cambiar los valores de origen y destino de la matriz originalArray con la ayuda de la fuente y el destino de la matriz de referenceArray si coincide, tome el valor del nombre y establezca ese valor en originalArray fuente y el destino de la matriz original.
const originalArray = [ { source: 'water', target: 'hero' }, { source: 'tata', target: 'ground' }, { source: 'livo', target: 'kia' }, { source: 'hero', target: 'sector' }, ];Salida esperada que se requiere:
const output1 = [ { source: 'animal', target: ['bike', 'vehicle'] }, { source: 'car', target: 'animal' }, { source: 'bike', target: 'car' }, { source: ['vehicle', 'bike'], target: 'car' }, ]; const output2 = [ { source: 'animal', target: 'bike' }, { source: 'animal', target: 'vehicle' }, { source: 'car', target: 'animal' }, { source: 'bike', target: 'car' }, { source: 'vehicle', target: 'car' }, { source: 'bike', target: 'car' }, ]; Cualquier salida es aceptable, sin embargo, se output2 la salida 2.
Estoy confundido sobre cómo lograr este formato sin tener conflictos de valores clave.
La siguiente puede ser una posible solución para lograr el objetivo deseado ( Salida-2 , de la pregunta anterior).
Fragmento de código
// get output in format-2 const getOutput2 = (refArr, arr) => ( arr.flatMap( // iterate using ".flatMap" to avoid nested result ({ source : osrc, target : otgt }) => ( refArr.filter( // filter for "source" using ref-arr ob => ob?.source.includes(osrc) || ob?.target.includes(osrc) ).sort( // sort descending to match order of output-2 (a, b) => b.name > a.name ? 1 : -1 ).flatMap( // for each "source", iterate using ".flatMap" ob => refArr.filter( // filter for "target" ob2 => ob2?.source.includes(otgt) || ob2?.target.includes(otgt) ).sort( // sort ascending to match order of output-2 (a, b) => a.name > b.name ? 1 : -1 ).map(ob2 => ({ // construct the desired objective array source: ob.name, target: ob2.name })) ) ) ) ); // reference array from question const referenceArray = [{ name: 'animal', source: ['duck', 'cat'], target: ['water', 'ground'] }, { name: 'car', source: ['tata', 'kia'], target: ['tiago', 'sector'] }, { name: 'bike', source: ['honda', 'hero'], target: ['livo', 'xtream'] }, { name: 'vehicle', source: ['honda', 'hero'], target: ['hard', 'soft'] }, ]; // original array from question const originalArray = [{ source: 'water', target: 'hero' }, { source: 'tata', target: 'ground' }, { source: 'livo', target: 'kia' }, { source: 'hero', target: 'sector' }, ]; // log the results to console for verification console.log(getOutput2(referenceArray, originalArray)); .as-console-wrapper { max-height: 100% !important; top: 0; }Explicación
Comentarios agregados en línea en el fragmento anterior