获取$ .each中返回的数组数

时间:2019-02-20 01:41:03

标签: javascript jquery

似乎是一个简单的问题,但无法在JQuery帮助中或SO的任何地方找到结果。如何获得从JSON数组返回的数组数的计数。 cust_id可能会返回2个数组或多达10个数组。

$.each(data, function() {
       if (this.cust_id == selectedProductAutonum) {
         invoices = this;                                         
         return true;
         alert(this.length);
       }
 });

示例JSON:

var myjson = [{
    "autonum": "20",
    "date": "2017-03-22",
    "customer": "Jeffs Music\r\n",
    "address": "Montgomery Station",
    "invoice": "20160929",
    "sales": "2416.00",
    "paid": "1000.00",
    "owed": "1416.00",
    "tax": "0.00",
    "misc": "2017-03-22 19:51:00",
    "cust_id": "15",
    "product": "Labor",
    "description": "Complete Install Labor only (DVR configuration, Install, Pow",
    "cost": "2150.00",
    "quantity": "1",
    "price": "2150.00",
    "timestamp": "2017-03-22 19:51:00"
}, {
    "autonum": "21",
    "date": "2017-03-22",
    "customer": "Jeffs Music\r\n",
    "address": "Montgomery Station",
    "invoice": "20160929",
    "sales": "2416.00",
    "paid": "1000.00",
    "owed": "1416.00",
    "tax": "0.00",
    "misc": "2017-03-22 19:51:00",
    "cust_id": "15",
    "product": "Home Depot",
    "description": "Home Depot Supplies for Shelf/mounts/braces/paint.",
    "cost": "128.50",
    "quantity": "1",
    "price": "128.50",
    "timestamp": "2017-03-22 19:51:00"
}, {
    "autonum": "22",
    "date": "2017-03-22",
    "customer": "Mikes Music\r\n",
    "address": "Montgomery Station",
    "invoice": "20160929",
    "sales": "2416.00",
    "paid": "1000.00",
    "owed": "1416.00",
    "tax": "0.00",
    "misc": "2017-03-22 19:51:00",
    "cust_id": "15",
    "product": "Cables",
    "description": "Cables and Connectors / power pigtails",
    "cost": "43.85",
    "quantity": "1",
    "price": "43.85",
    "timestamp": "2017-03-22 19:51:00"
}, {
    "autonum": "23",
    "date": "2017-03-22",
    "customer": "Jeffs Music\r\n",
    "address": "Montgomery Station",
    "invoice": "20160929",
    "sales": "2416.00",
    "paid": "1000.00",
    "owed": "1416.00",
    "tax": "0.00",
    "misc": "2017-03-22 19:51:00",
    "cust_id": "10",
    "product": "Cables",
    "description": "Cables and connectors / hardware used in November 2016",
    "cost": "45.55",
    "quantity": "1",
    "price": "45.55",
    "timestamp": "2017-03-22 19:51:00"
}, {
    "autonum": "24",
    "date": "2017-03-22",
    "customer": "Jeffs Music\r\n",
    "address": "Montgomery Station",
    "invoice": "20160929",
    "sales": "2416.00",
    "paid": "1000.00",
    "owed": "1416.00",
    "tax": "0.00",
    "misc": "2017-03-22 19:51:00",
    "cust_id": "10",
    "product": "Cables",
    "description": "Extension power supply cables",
    "cost": "43.12",
    "quantity": "1",
    "price": "43.12",
    "timestamp": "2017-03-22 19:51:00"
}]

2 个答案:

答案 0 :(得分:3)

您可以使用函数filter获取具有cust_id === "10"的对象数组。

var myjson = [{    "autonum": "20",    "date": "2017-03-22",    "customer": "Jeffs Music\r\n",    "address": "Montgomery Station",    "invoice": "20160929",    "sales": "2416.00",    "paid": "1000.00",    "owed": "1416.00",    "tax": "0.00",    "misc": "2017-03-22 19:51:00",    "cust_id": "15",    "product": "Labor",    "description": "Complete Install Labor only (DVR configuration, Install, Pow",    "cost": "2150.00",    "quantity": "1",    "price": "2150.00",    "timestamp": "2017-03-22 19:51:00"}, {    "autonum": "21",    "date": "2017-03-22",    "customer": "Jeffs Music\r\n",    "address": "Montgomery Station",    "invoice": "20160929",    "sales": "2416.00",    "paid": "1000.00",    "owed": "1416.00",    "tax": "0.00",    "misc": "2017-03-22 19:51:00",    "cust_id": "15",    "product": "Home Depot",    "description": "Home Depot Supplies for Shelf/mounts/braces/paint.",    "cost": "128.50",    "quantity": "1",    "price": "128.50",    "timestamp": "2017-03-22 19:51:00"}, {    "autonum": "22",    "date": "2017-03-22",    "customer": "Mikes Music\r\n",    "address": "Montgomery Station",    "invoice": "20160929",    "sales": "2416.00",    "paid": "1000.00",    "owed": "1416.00",    "tax": "0.00",    "misc": "2017-03-22 19:51:00",    "cust_id": "15",    "product": "Cables",    "description": "Cables and Connectors / power pigtails",    "cost": "43.85",    "quantity": "1",    "price": "43.85",    "timestamp": "2017-03-22 19:51:00"}, {    "autonum": "23",    "date": "2017-03-22",    "customer": "Jeffs Music\r\n",    "address": "Montgomery Station",    "invoice": "20160929",    "sales": "2416.00",    "paid": "1000.00",    "owed": "1416.00",    "tax": "0.00",    "misc": "2017-03-22 19:51:00",    "cust_id": "10",    "product": "Cables",    "description": "Cables and connectors / hardware used in November 2016",    "cost": "45.55",    "quantity": "1",    "price": "45.55",    "timestamp": "2017-03-22 19:51:00"}, {    "autonum": "24",    "date": "2017-03-22",    "customer": "Jeffs Music\r\n",    "address": "Montgomery Station",    "invoice": "20160929",    "sales": "2416.00",    "paid": "1000.00",    "owed": "1416.00",    "tax": "0.00",    "misc": "2017-03-22 19:51:00",    "cust_id": "10",    "product": "Cables",    "description": "Extension power supply cables",    "cost": "43.12",    "quantity": "1",    "price": "43.12",    "timestamp": "2017-03-22 19:51:00"}],
    selectedProductAutonum = "10",
    result = myjson.filter(({cust_id}) => cust_id === selectedProductAutonum);
    
console.log("Length:", result.length);

selectedProductAutonum = "15";
result = myjson.filter(({cust_id}) => cust_id === selectedProductAutonum);

console.log("Length:", result.length);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

您可以使用Array.prototype.filter()

const filterProp = (data, prop, match) => data.filter(({[prop]:p}) => p == match)
let selectedProductAutonum = "15"
let {length: len} = filterProp(myjson, "cust_id", selectedProductAutonum)
console.log(len)