javascript - 如何从存储在数组中的对象返回键

标签 javascript arrays function object

如何从对象集合中返回 key 并使用配方计算它?我陷入了 for 循环并已经尝试使用 .reduce() 。但我理解得还不够好。有人可以帮助我或向我解释吗?解决这个问题的最佳方法是什么?

var soup = { 
            potato: 3,
            onion: 1,
            corn: 5
            };

var gratin = {
            meat: 2,
            onion: 2,
            pea: 5
            };

var pizza = {
            cheese: 1,
            tomato: 3,
            oregano: 2
        };


var edoka = { // in a store, the values are the prices per ingredient
            cheese: 8,
            corn: 3,
            meat: 6,
            onion: 4,
            pea: 1,
            oregano: 7,
            potato: 5,
            tomato: 6
        };

        var were = {
            cheese: 6,
            corn: 2,
            meat: 9,
            onion: 5,
            pea: 2,
            oregano: 6,
            potato: 3,
            tomato: 3
        };

        var brutto = {
            cheese: 6,
            corn: 2,
            meat: 9,
            onion: 5,
            pea: 2,
            oregano: 8,
            potato: 3,
            tomato: 4
        };

        var allStores = { // this is an example of a "storeCollection"
            Brutto: brutto,
            Edoka: edoka,
            Were: were,
        };


function cheapestStoreForRecipe(recipe, storeCollection){
            // make it return the key for the store in storeCollection
            // that has the cheapest total cost for recipe. Feel free
            // to use costOfRecipe inside this function!  
        }

最佳答案

您可以使用Object.keys()reduce:

var soup = { potato:3, onion:1, corn:5 },
    gratin = { meat:2, onion:2, pea:5 },
    pizza = { cheese:1, tomato:3, oregano:2 };
    
var edoka = { cheese:8, corn:3, meat:6, onion:4, pea:1, oregano:7, potato:5, tomato:6 },
    were = { cheese:6, corn:2, meat:9, onion:5, pea:2, oregano:6, potato:3, tomato:3 },
    brutto = { cheese:6, corn:2, meat:9, onion:5, pea:2, oregano:8, potato:3, tomato:4 };

var allStores = {
  Brutto: brutto,
  Edoka: edoka,
  Were: were,
};


function cheapestStoreForRecipe(recipe, storeCollection) {
  var cheapest = Object.keys(storeCollection).reduce((result, name) => {
    var total = getStoreRecipePrice(storeCollection[name], recipe);
    if (result === null || total < result.total) {
      return {name, total};
    }
    return result;
  }, null);
  return cheapest;
}

function getStoreRecipePrice(store, recipe) {
  //                                                          price    *  quantity
  return Object.keys(recipe).reduce((total, key) => total + store[key] * recipe[key], 0);
}

console.log('soup: ', cheapestStoreForRecipe(soup, allStores));
console.log('gratin: ', cheapestStoreForRecipe(gratin, allStores));
console.log('pizza: ', cheapestStoreForRecipe(pizza, allStores));

关于javascript - 如何从存储在数组中的对象返回键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60682789/

相关文章:

Javascript onclick动态for循环获取索引

javascript - 将鼠标悬停在文本上,图像淡入淡出

c - 为什么 "while"的一个主体工作而另一个不工作?

javascript - JavaScript 中的 str.fun()/str.fun/fun(str) 有什么区别?

php - 有人可以向我解释一下 PHP 中的 pack() 函数吗?

javascript - 我可以在 JavaScript 中使用浏览器的自动换行吗?

javascript - 不要使用publishReplay缓存错误

java - Java中如何根据比率对两个数组进行排序?

arrays - 从CSV加载两个数组

python - 简单的减法会导致不同数组形状的广播问题