第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

僅提取數(shù)組中的多個(gè)對(duì)象

僅提取數(shù)組中的多個(gè)對(duì)象

牛魔王的故事 2023-08-18 14:18:17
我剛剛與 Postman 合作創(chuàng)建一些測(cè)試。大多數(shù)get響應(yīng)由大塊數(shù)組組成,其中包含大量對(duì)象(為了便于閱讀,我只留下了兩個(gè)屬性,這些對(duì)象有 20 多個(gè)屬性)。我有一個(gè)腳本可以讀取整個(gè)響應(yīng)以獲取正確的數(shù)據(jù),然后返回結(jié)果。如何在達(dá)到一定數(shù)量的對(duì)象時(shí)停止腳本?[   {      "username": "",      "active": ""   },   {      "username": "",      "active": ""   }]
查看完整描述

2 回答

?
拉莫斯之舞

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊

也許這對(duì)你有幫助(我不知道我是否理解得很好)。


但是使用filter您可以獲取具有一個(gè)屬性的值(您可以匹配您想要的任何內(nèi)容)并且使用slice您將獲得前 N 個(gè)值。


因此,您不必迭代整個(gè)列表,而可以僅檢查這些值。


另外,如果您只想要匹配某個(gè)條件的元素?cái)?shù)量,則只需要使用filter和 length 。


var array = [

   {

      "username": "1",

      "active": true

   },

   {

      "username": "2",

      "active": false

   },

   {

      "username": "3",

      "active": true

   }

]


var total = 1 // total documents you want

var newArray = array.filter(e => e.active).slice(0, total);


console.log(newArray)


//To know the length of elements that match the condition:

var length = array.filter(e => e.active).length

console.log(length)


查看完整回答
反對(duì) 回復(fù) 2023-08-18
?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

看看下面的代碼是否有幫助


function processLongArray() {


  var myLongArray = [{

    "username": "active"

  }, {

    "username": "active"

  }, {

    "username": "inactive"

  }]; // and many more elements in the array


  var count = 0;

  var targetCount = 1; // stop after this number of objects

  for (var i = 0; i < myLongArray.length; i++) {

    var arrayItem = myLongArray[i];


    // condition to test if the arrayItem is considered in count

    // If no condition needed, we can directly increment the count

    if (arrayItem.username === "active") {

      count++;

    }


    if (count >= targetCount) {

      console.log("OK we are done! @ " + count);

      return count; // or any other desired value

    }

  }

}


processLongArray();


查看完整回答
反對(duì) 回復(fù) 2023-08-18
  • 2 回答
  • 0 關(guān)注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)