在JSON中找到最小的孩子

时间:2021-07-20 17:54:20

Consider a JSON like this

考虑像这样的JSON

{
    "products": {
        "c1": {
            "stock": 100
        },
        "c2": {
            "stock": 200
        },
        "c3": {
            "stock": 300
        },
        "c4": {
            "stock": 400
        },
        "c5": {
            "stock": 500
        }
    }
}

To find minimum of stock, i wrote code like this

为了找到最少的库存,我写了这样的代码

 var minStock=Math.min(
      products.c1.stock,
      products.c2.stock,
      products.c3.stock,
      products.c4.stock,
      products.c5.stock
      )
  console.log(minStock);

now i want to know which is minimum whether c1 ,c2,c3,c4 or c5 based on stock values,say in this case

现在我想知道基于库存值的c1,c2,c3,c4或c5是最小的,在这种情况下说

console.log("c1 is minimum with stock 100");

3 个解决方案

#1


5  

This is a solution with Array#reduce(). It returns equal references, too:

这是Array#reduce()的解决方案。它也返回相同的引用:

var object = { "products": { "c1": { "stock": 200 }, "c2": { "stock": 200 }, "c3": { "stock": 300 }, "c4": { "stock": 400 }, "c5": { "stock": 500 } } },
    result = function (o) {
        var keys = Object.keys(o);
        return keys.reduce(function (r, k) {
            if (o[k].stock < o[r[0]].stock) {
                return [k];
            }
            if (o[k].stock === o[r[0]].stock) {
                r.push(k);
            }
            return r;
        }, [keys.shift()]);
    }(object.products);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
document.write(result.join(', ') + ' ' + (result.length === 1 ? 'is' : 'are') + ' minimum with stock à '+ object.products[result[0]].stock);

#2


2  

use Object.keys and array#reduce for quick and easy solution

使用Object.keys和array#reduce进行快速简便的解决方案

var min = Object.keys(products).reduce(function(min, item) {
    return products[item].stock < products[min].stock ? item : min;
});
console.log(min, 'is minimum with stock', products[min].stock);

Or Object.keys, array#map and array#sort

或Object.keys,数组#map和数组#sort

var min = Object.keys(products).map(function(item) {
    return {product: item, stock: products[item].stock}
}).sort(function(a, b) {
    return a.stock - b.stock;
})[0]
console.log(min.product, 'is minimum with stock', min.stock);

#3


1  

The alternative with Array.sort function:

使用Array.sort函数的替代方法:

var obj = {
        "products": {            
            "c2": {
                "stock": 200
            },
            "c3": {
                "stock": 300
            },
            "c4": {
                "stock": 400
            },
            "c5": {
                "stock": 500
            },
            "c1": {
                "stock": 100
            },
        }
    };

    var products = Object.keys(obj.products);
    products.sort(function(a,b){
        return obj.products[a].stock - obj.products[b].stock;
    });
    console.log(products[0] + " is minimum with stock " + obj.products[products[0]].stock);
    // c1 is minimum with stock 100

https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

#1


5  

This is a solution with Array#reduce(). It returns equal references, too:

这是Array#reduce()的解决方案。它也返回相同的引用:

var object = { "products": { "c1": { "stock": 200 }, "c2": { "stock": 200 }, "c3": { "stock": 300 }, "c4": { "stock": 400 }, "c5": { "stock": 500 } } },
    result = function (o) {
        var keys = Object.keys(o);
        return keys.reduce(function (r, k) {
            if (o[k].stock < o[r[0]].stock) {
                return [k];
            }
            if (o[k].stock === o[r[0]].stock) {
                r.push(k);
            }
            return r;
        }, [keys.shift()]);
    }(object.products);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
document.write(result.join(', ') + ' ' + (result.length === 1 ? 'is' : 'are') + ' minimum with stock à '+ object.products[result[0]].stock);

#2


2  

use Object.keys and array#reduce for quick and easy solution

使用Object.keys和array#reduce进行快速简便的解决方案

var min = Object.keys(products).reduce(function(min, item) {
    return products[item].stock < products[min].stock ? item : min;
});
console.log(min, 'is minimum with stock', products[min].stock);

Or Object.keys, array#map and array#sort

或Object.keys,数组#map和数组#sort

var min = Object.keys(products).map(function(item) {
    return {product: item, stock: products[item].stock}
}).sort(function(a, b) {
    return a.stock - b.stock;
})[0]
console.log(min.product, 'is minimum with stock', min.stock);

#3


1  

The alternative with Array.sort function:

使用Array.sort函数的替代方法:

var obj = {
        "products": {            
            "c2": {
                "stock": 200
            },
            "c3": {
                "stock": 300
            },
            "c4": {
                "stock": 400
            },
            "c5": {
                "stock": 500
            },
            "c1": {
                "stock": 100
            },
        }
    };

    var products = Object.keys(obj.products);
    products.sort(function(a,b){
        return obj.products[a].stock - obj.products[b].stock;
    });
    console.log(products[0] + " is minimum with stock " + obj.products[products[0]].stock);
    // c1 is minimum with stock 100

https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/sort