检查对象数组中是否存在属性而不对其进行循环

时间:2017-05-01 16:32:21

标签: javascript ecmascript-6

我有一个对象数组:

let arr = [{"id":1, "name": "abc"},{"id":2, "name": "fsd"},{"id":3, "name": "fasd"},{"id":4, "name": "fsdfas"},{"id":5, "name": "fad"},{"id":6, "name": "fasdf"},{"id":7, "name": "fasd"},{"id":8, "name": "fasdf"},{"id":9, "name": "fasdfs"},{"id":10, "name": "abc"}]

如何在不使用任何类型的循环语句的情况下检查对象数组中是否存在属性“id”?一旦找到,返回一些字符串或任何东西,例如。

3 个答案:

答案 0 :(得分:2)

在一般情况下,你不能;某种循环结构是必要的。循环可能在您的代码中,也可能不在您的代码中,但它会在那里。例如,即使您使用数组并通过JSON.stringify运行它然后尝试在结果上使用正则表达式,A)这将是一个坏主意™: - ),并且B)仍然会有循环(在JSON.stringify中,也可能在正则表达式处理中)。而且,开销会比仅仅进行自己的循环要高得多。

如果 ,在您的情况下,您知道提前该对象都具有相同的属性集,那么您只需检查第一个:

if ("id" in arr[0])

......但是,只有事先知道它们都具有相同的属性。

拉伸一个点,in必须检查对象,如果找不到属性,则必须检查其原型 - 可能包含一个循环。 :-)(当然,任何属性访问都可能必须这样做。)所以你可能更喜欢

if (arr[0].hasOwnProperty("id"))

...理论上,也可能涉及循环通过原型来找到hasOwnProperty方法(实际上,在现代引擎上,它不会)。

答案 1 :(得分:0)

您可以使用正则表达式并检查字符串化数组。



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="PopUp" class="overlay">
  <div class="PopUp">
    <div class="timer" id="timer"><img src="http://i.imgur.com/87XaOWA.png">
      <p class="close-message" id="close-message"></p>
    </div>
    <a class="close" href="#">&#10005;</a>
    <div class="content" id="content">
      <div id="p1" class="p1" style="overflow: hidden; position: relative; width: 885px; height: 878px;">
        <!-- Begin page background -->
        <div id="pg1Overlay" style="width:100%; height:100%; position:absolute; z-index:1; background-color:rgba(0,0,0,0); -webkit-user-select: none;"></div>
        <div id="pg1" class="pg1" style="-webkit-user-select: none;"><img src="http://i.imgur.com/vcY42CW.png" id="pdf1" class="pdf1"></img>
        </div>
        <!-- End page background -->

        <!-- Begin text definitions (Positioned/styled in CSS) -->
        <div id="t1_1" class="t s1_1">Player Selection Criteria Evaluator Cue Card</div>
        <div id="t2_1" class="t s2_1">Skating – speed, quickness, technique</div>
        <div id="t3_1" class="t s3_1">♦</div>
        <div id="t4_1" class="t s4_1">Forward and Backward</div>
        <div id="t5_1" class="t s3_1">♦</div>
        <div id="t6_1" class="t s3_1">♦</div>
        <div id="t7_1" class="t s3_1">♦</div>
        <div id="t8_1" class="t s4_1">Turn both directions</div>
        <div id="t9_1" class="t s4_1">Stop both directions</div>
        <div id="ta_1" class="t s4_1">Are they in a good position for stability and strength</div>
        <div id="tb_1" class="t s2_1">Passing – technique, control, vision</div>
        <div id="tc_1" class="t s3_1">♦</div>
        <div id="td_1" class="t s3_1">♦</div>
        <div id="te_1" class="t s3_1">♦</div>
        <div id="tf_1" class="t s3_1">♦</div>
        <div id="tg_1" class="t s4_1">Forehand and Backhand</div>
        <div id="th_1" class="t s4_1">To moving and stationary target</div>
        <div id="ti_1" class="t s4_1">Vision – do they take a look and select best option</div>
        <div id="tj_1" class="t s4_1">Advanced – board passes, chips, saucer passes</div>
        <div id="tk_1" class="t s2_1">Puck Control – technique, open ice, confined space</div>
        <div id="tl_1" class="t s3_1">♦</div>
        <div id="tm_1" class="t s3_1">♦</div>
        <div id="tn_1" class="t s3_1">♦</div>
        <div id="to_1" class="t s3_1">♦</div>
        <div id="tp_1" class="t s4_1">Open carry with speed</div>
        <div id="tq_1" class="t s4_1">Execute dekes and fakes on the 1 on 1</div>
        <div id="tr_1" class="t s4_1">Can they handle the puck in traffic and tight space</div>
        <div id="ts_1" class="t s4_1">Ability to maintain control while being stick checked</div>
        <div id="tt_1" class="t s2_1">Shooting – technique, accuracy, velocity</div>
        <div id="tu_1" class="t s3_1">♦</div>
        <div id="tv_1" class="t s3_1">♦</div>
        <div id="tw_1" class="t s3_1">♦</div>
        <div id="tx_1" class="t s4_1">Velocity</div>
        <div id="ty_1" class="t s3_1">♦</div>
        <div id="tz_1" class="t s4_1">Accuracy</div>
        <div id="t10_1" class="t s3_1">♦</div>
        <div id="t11_1" class="t s4_1">Forehand and backhand</div>
        <div id="t12_1" class="t s4_1">Wrist shot, snap shot, slap shot</div>
        <div id="t13_1" class="t s4_1">Shot Selection – do they select the best shot for the opportunity?</div>
        <div id="t14_1" class="t s2_1">Game Understanding – Principles of Offence and Defence</div>
        <div id="t15_1" class="t s3_1">♦</div>
        <div id="t16_1" class="t s3_1">♦</div>
        <div id="t17_1" class="t s3_1">♦</div>
        <div id="t18_1" class="t s3_1">♦</div>
        <div id="t19_1" class="t s4_1">Player understands positional play</div>
        <div id="t1a_1" class="t s4_1">Player supports the puck on the defensive and offensive side of puck</div>
        <div id="t1b_1" class="t s4_1">Player communicates with teammates</div>
        <div id="t1c_1" class="t s4_1">Player has the ability to read and react.</div>
      </div>
    </div>
    <div id="Print" class="Print" align="center">
      <button onclick="printDiv()"><img src="http://i.imgur.com/HcXNoMC.png" /></button>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

最接近的是创建一个函数来对数组进行字符串化,然后使用正则表达式查找特定的ID值,如下所示:

正则表达式.test()是(内部)迭代JSON字符串中的字符,因此在基础级别,您要求的是不可能的。

let arr = [{"id":1, "name": "abc"},{"id":2, "name": "fsd"},{"id":3, "name": "fasd"},{"id":4, "name": "fsdfas"},{"id":5, "name": "fad"},{"id":6, "name": "fasdf"},{"id":7, "name": "fasd"},{"id":8, "name": "fasdf"},{"id":9, "name": "fasdfs"},{"id":10, "name": "abc"}];

function idExistsInArray(array, id) {
    var regex = new RegExp("\"id\":" + id + "(,|})");
    return regex.test(JSON.stringify(array));
}

console.log(idExistsInArray(arr, 1));
console.log(idExistsInArray(arr, 3));
console.log(idExistsInArray(arr, 55));