我如何通过linq获取数组项

时间:2019-04-12 09:33:32

标签: javascript linq

Variables.PolicyCustomerAddresses

(3) [{…}, {…}, {…}]
0: {ADRES_EK: null, ADRES_ID1: 885843,  ADRES_TEXT: "XXXX", MUSTERI_ADRES_ID: "333", …}

1: {ADRES_EK: null, ADRES_ID1: 890997, ADRES_TEXT: "YYYY", MUSTERI_ADRES_ID: "222", …}

2: {ADRES_EK: null, ADRES_ID1: 890902,  ADRES_TEXT: "ZZZZ", MUSTERI_ADRES_ID: "111", …}
length: 3

correspondenceAdress == 222

像这样,我想用linq的MUSTERI_ADRES_ID == 222代替

var adreeess = Variables.PolicyCustomerAddresses;
                var adrx = Enumerable.From(adreeess)
                        .Where("$.MUSTERI_ADRES_ID === correspondenceAdress ")
                        .FirstOrDefault(null)
                        .First();

1 个答案:

答案 0 :(得分:1)

MUSTERI_ADRES_ID必须是字符串,作为给定数据中的值。

var Variables = { PolicyCustomerAddresses: [{ ADRES_EK: null, ADRES_ID1: 885843,  ADRES_TEXT: "XXXX", MUSTERI_ADRES_ID: "333" }, { ADRES_EK: null, ADRES_ID1: 890997, ADRES_TEXT: "YYYY", MUSTERI_ADRES_ID: "222" }, { ADRES_EK: null, ADRES_ID1: 890902,  ADRES_TEXT: "ZZZZ", MUSTERI_ADRES_ID: "111" }] },
    correspondenceAdress = "222", // string!
    result = Enumerable.From(Variables.PolicyCustomerAddresses)
        .Where("$.MUSTERI_ADRES_ID === correspondenceAdress")
        .DefaultIfEmpty(null)
        .First();

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>