How to obtain a single object from the comparison of the smallest of the values between an array of objects?

Asked

Viewed 46 times

1

I’m unable to make a selection that looks for which is lower price and also the shorter delivery time of a JSON.

I need to return the complete object. Something like:

{
  id: 2,
  price: 10,
  available: true,
  delivery_time: 7
}

But my current code returns only the values:

let response = [
  { id: 1, price: 10, available: true, delivery_time: 20 },
  { id: 2, price: 10, available: true, delivery_time: 7 },
  { id: 3, price: 30, available: true, delivery_time: 5 },
  { id: 4, price: 40, available: true, delivery_time: 1 }
];

let a = response.map(function (v) {
  return v.price;
});
let b = response.map(function (v) {
  return v.delivery_time;
});

let aa = Math.min.apply(null, a);
let bb = Math.min.apply(null, b);

console.log(aa);
console.log(bb);

2 answers

2

The problem is, when applying Math.min to the result of mapping the original array to the prices and delivery times, you lose the reference to the desired object. In this situation, although you have the value the lowest price and the lowest delivery time, you have no reference to the object to which they now belonged.

Therefore, you should look for a way to keep reference to objects. For this, you can use a for to iterate over each object, creating two objects (external to the for) to store which is the least priced item and the least delivery time item.

As the loop is iterating, you check whether the current meets the criteria better than the previous one and, if positive, changes the "reference object".

Something like that:

const response = [
  { id: 1, price: 10, available: true, delivery_time: 20 },
  { id: 2, price: 10, available: true, delivery_time: 7 },
  { id: 3, price: 30, available: true, delivery_time: 5 },
  { id: 4, price: 40, available: true, delivery_time: 1 }
];

let minPrice = response[0];
let minDeliveryTime = response[0];

// Como já começamos no primeiro elemento da lista, podemos começar a iterar
// a partir do segundo (índice 1):
for (let i = 1; i < response.length; i++) {
  const current = response[i];

  // Se o preço for menor que o atual, substituimos:
  if (current.price < minPrice.price) {
    minPrice = current;
  }

  // Se o tempo de entrega for menor que o atual, substituimos:
  if (current.delivery_time < minDeliveryTime.delivery_time) {
    minDeliveryTime = current;
  }
}

console.log(minPrice);
console.log(minDeliveryTime);

Probably have to encapsulate this in a relatively generic function, but I leave as a challenge. If you have time, I add here later. :-)

  • Wow, thank you so much for the help. I will seek to solve this challenge kk

  • You are welcome, @VHB. See on [tour] how to thank here. You can accept the answer and vote positive if you feel it is relevant. :)

0

// Recebe as opções de frete disponiveis  
const frete_opcoes = [
  { id: 1, price: 10, available: true, delivery_time: 8 },
  { id: 2, price: 10, available: true, delivery_time: 7 },
  { id: 3, price: 30, available: true, delivery_time: 5 },
  { id: 4, price: 40, available: true, delivery_time: 1 }
];
// Filtra pelo menor preço
let menor_preco = frete_opcoes[0];
for (let i = 1; i < frete_opcoes.length; i++) {
const atual = frete_opcoes[i];
  // Se o preço for menor que o atual, substituimos:
  if (atual.price < menor_preco.price) {
    menor_preco = atual;
  }

}
var menortempo = frete_opcoes.filter(function(envio) {
  var preco = Number(envio.price);
  return preco <= menor_preco.price;
});

let MelhorFrete = menortempo[0];
for (let i = 1; i < menortempo.length; i++) {
  const selecionaAtual = menortempo[i];
  if (selecionaAtual.delivery_time < MelhorFrete.delivery_time) {
    MelhorFrete = selecionaAtual;
  }

}
console.log(MelhorFrete);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.