Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
How to make an array of specific values from an array object?

When I get results from an API with this function

let recipeArray = async function getRecipeByPreference() {
  try {
    let results = await axios.get('https://api.spoonacular.com/recipes/complexSearch?number=5&apiKey=*****', {
      headers: {
        "Content-Type": "application/json"
      }
    })

    return recipeArray = results.data.results.slice(0, 5);

  } catch (e) {
    console.error(e);
  }
}

These are the results returned in recipeArray:

0: {id: 716426, title: 'Cauliflower, Brown Rice, and Vegetable Fried Rice', image: 'https://spoonacular.com/recipeImages/716426-312x231.jpg', imageType: 'jpg'}
1: {id: 715594, title: 'Homemade Garlic and Basil French Fries', image: 'https://spoonacular.com/recipeImages/715594-312x231.jpg', imageType: 'jpg'}
2: {id: 715497, title: 'Berry Banana Breakfast Smoothie', image: 'https://spoonacular.com/recipeImages/715497-312x231.jpg', imageType: 'jpg'}
3: {id: 644387, title: 'Garlicky Kale', image: 'https://spoonacular.com/recipeImages/644387-312x231.jpg', imageType: 'jpg'}
4: {id: 716268, title: 'African Chicken Peanut Stew', image: 'https://spoonacular.com/recipeImages/716268-312x231.jpg', imageType: 'jpg'}

I want to put the first 5 ID numbers in the recipeArray, so I get this:

recipeArray = [716426,715594,715497,644387,716268]

and console.log(recipeArray[0]); will give me 716426

I know there are similar questions here on stack overflow but I can't make the solutions work for me.

Any help would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Just change results.data.results.slice(0,5); to results.data.results.slice(0,5).map(({id})=> id); and it should work ;)

const results = [{
    id: 716426,
    title: 'Cauliflower, Brown Rice, and Vegetable Fried Rice',
    image: 'https://spoonacular.com/recipeImages/716426-312x231.jpg',
    imageType: 'jpg'
  },
  {
    id: 715594,
    title: 'Homemade Garlic and Basil French Fries',
    image: 'https://spoonacular.com/recipeImages/715594-312x231.jpg',
    imageType: 'jpg'
  }, {
    id: 715497,
    title: 'Berry Banana Breakfast Smoothie',
    image: 'https://spoonacular.com/recipeImages/715497-312x231.jpg',
    imageType: 'jpg'
  }, {
    id: 644387,
    title: 'Garlicky Kale',
    image: 'https://spoonacular.com/recipeImages/644387-312x231.jpg',
    imageType: 'jpg'
  }, {
    id: 716268,
    title: 'African Chicken Peanut Stew',
    image: 'https://spoonacular.com/recipeImages/716268-312x231.jpg',
    imageType: 'jpg'
  }
];

const idArray = results.slice(0, 5).map(({
  id
}) => id);

console.log(idArray);

about 4 years ago · Juan Pablo Isaza Relatório

0

You could map the Array you got and make it only have the recipe Ids

return recipeArray = results.data.results.slice(0,5).map(elem => elem.id)

Map() is an Array method that changes the array to match the function passed, in our case we want to turn every recipe object into just its ID.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda