Home > Enterprise >  How to find elephant with the lowest value in object
How to find elephant with the lowest value in object

Time:05-20

Hello I have a question how can I find elephant with the lowest value from object? I need to find elephant with the value of "minimum" variable. I mean I want to get the whole object.

 let numberOfElephants = 6;
let massOfElephants = [2400, 2000, 1200, 2400, 1600, 4000];
let startOrder = [1, 4, 5, 3, 6, 2];
let endOrder = [5, 3, 2, 4, 6, 1];
    
    
      
    
        const elephantObject = [];
          startOrder.forEach((order, i) => {
            elephantObject.push({ [order]: massOfElephants[i] });
          });
        
        for (let i = 0; i < numberOfElephants; i  ) {
            for (let j = 0; j < numberOfElephants; j  ) {
 const indexOfCorrect = proposedOrder.findIndex(
          (num) => num === endOrder[j]
        );
        const itemAtGivenOrder = startOrder[indexOfCorrect];
            
                const [value] = Object.values(elephantObject[i]);
                const [valueOfStarter] = Object.values(elephantObject[startOrder[i]]);
                const minimum = Math.min(
                  elephantAtGivenCorrectIndex[itemAtGivenOrder],
                  value
                );

CodePudding user response:

I'm guessing at the intended content of massOfElephants here:

let startOrder = [1, 4, 5, 3, 6, 2];
let massOfElephants = [100, 200, 500, 2, 8, 9000]

const elephantObject = [];
startOrder.forEach((order, i) => {
  elephantObject.push({
    [order]: massOfElephants[i]
  });
});

let leastMassyElephant = Math.min(...massOfElephants)

let output = elephantObject.filter(item => {
  return item[Object.keys(item)[0]] === leastMassyElephant
})

console.log(output)

  • Related