Diego
Diego

Reputation: 268

Construct an array based on two separate arrays

I am trying to construct an array based on two separate arrays such as the following example:

const breakfast = [
   { dishId: 23, name: 'Pasta'}
]

const ingredients = [
   // ...
   { ingrId: 13, name: 'Tomato' },
   { ingrId: 29, name: 'Beef' }
]

// connecting table
const breakfastDishIngredients = [
   { id: 1, dishId: 23, ingrId: 13 },
   { id: 1, dishId: 23, ingrId: 29 }
]

The new array should be the breakfast array with an additional ingredients key for every element. Like this:

const newBreakfast = [
   { dishId: 23, name: 'Pasta', ingredients: [
                { ingrId: 13, name: 'Tomato' },
                { ingrId: 29, name: 'Beef' }
              ]}
] 

I am trying the following but its not working:

let newBreakfast = []

for(let i in breakfastDishIngredients) {
    _breakfast = breakfast.map(item => {
        return { ...item, ingredients: ingredients.filter(el => item.id === breakfastDishIngredients[i][0].dish_id && el.id === breakfastDishIngredients[i][0].ingredient_id) }
    })
}

I appreciate any help.

Upvotes: 1

Views: 100

Answers (4)

iAmOren
iAmOren

Reputation: 2804

const breakfast = [
   { dishId: 23, name: 'Pasta'}
];
const ingredients = [
   // ...
   { ingrId: 13, name: 'Tomato' },
   { ingrId: 29, name: 'Beef' }
];
const breakfastDishIngredients = [
   { id: 1, dishId: 23, ingrId: 13 },
   { id: 1, dishId: 23, ingrId: 29 }
];

let newBreakfast=breakfast.map(
  dish=>(
    {
      dishId:dish.dishId, name:dish.name, ingredients:
      breakfastDishIngredients.filter(
        ingredient=>ingredient.dishId==dish.dishId
      ).map(
        filtered=>(
          ingredients.filter(
            ingredient=>ingredient.ingrId==filtered.ingrId
          )
        )
      ).flat()
    }
  )
)

console.log(newBreakfast);

Upvotes: 1

jdc
jdc

Reputation: 384

Solution with Reducer and some utils for more reusability.

const breakfast = [
   { dishId: 23, name: 'Pasta'}
]

const ingredients = [
   // ...
   { ingrId: 13, name: 'Tomato' },
   { ingrId: 29, name: 'Beef' }
]

// connecting table
const breakfastDishIngredients = [
   { id: 1, dishId: 23, ingrId: 13 },
   { id: 1, dishId: 23, ingrId: 29 },
   { id: 1, dishId: 24, ingrId: 29 }
]

//utils
const lookUp = idString => arr => targetId => arr.filter(el => el[idString] == targetId)

// FP specialization
const lookUpIngrId = lookUp("ingrId")(ingredients);
const lookUpDishId = lookUp("dishId")(breakfast);

const getFirstOneOrNull = arr => Array.isArray(arr) && arr.length === 1 ? arr[0] : null;

//reducer
const reducerBreakFast = (acc, currentValue) => {
  const id = getFirstOneOrNull(lookUpDishId(currentValue.dishId))
  if(!id){
    return acc;
  }
  const ingrValue = getFirstOneOrNull(lookUpIngrId(currentValue.ingrId))
  acc.has(id) ? acc.set(id, [...acc.get(id), ingrValue]) : acc.set(id, [ingrValue])
  return acc;
}

const mapNewBreakfast = breakfastDishIngredients.reduce(reducerBreakFast, new Map())

const mapToArray = dataMap => Array.from(dataMap.keys()).map(key => ({
  ...key,
  ingredients: dataMap.get(key)
}))

const newBreakfast = mapToArray(mapNewBreakfast);

Output:

{
  dishId: 23,
  name: 'Pasta',
  ingredients: [ { ingrId: 13, name: 'Tomato' }, { ingrId: 29, name: 'Beef' } ]
}

Upvotes: 0

Christian
Christian

Reputation: 7862

Try this:

const breakfast = [
   { dishId: 23, name: 'Pasta'}
]

const ingredients = [
   // ...
   { ingrId: 13, name: 'Tomato' },
   { ingrId: 29, name: 'Beef' }
]

// connecting table
const breakfastDishIngredients = [
   { id: 1, dishId: 23, ingrId: 13 },
   { id: 1, dishId: 23, ingrId: 29 }
]

const result = breakfast.map(br => {
  const ingredientsFiltered = breakfastDishIngredients
                                .filter(brDiIngr => brDiIngr.dishId === br.dishId)
                                .map(el => ingredients.find(ingr => ingr.ingrId === el.ingrId));
  return { ...br, ingredients: ingredientsFiltered };
});

console.log(result);

Upvotes: 1

Ahmad Suddle
Ahmad Suddle

Reputation: 190

you can do something like this

const breakfast = [ { dishId: 23, name: 'Pasta' } ];

const ingredients = [
    // ...
    { ingrId: 13, name: 'Tomato' },
    { ingrId: 29, name: 'Beef' }
];

// connecting table
const breakfastDishIngredients = [ { id: 1, dishId: 23, ingrId: 13 }, { id: 1, dishId: 23, ingrId: 29 } ];

const newBreakfast = [
    {
        dishId: 23,
        name: 'Pasta',
        ingredients: [ { ingrId: 13, name: 'Tomato' }, { ingrId: 29, name: 'Beef' } ]
    }
];

let result = breakfast.map((item) => ({ ...item, ingredients: ingredients }));
console.log(result);

Upvotes: 0

Related Questions