Searching for items in a JSON array Using Node (preferably without iteration)

后端 未结 6 911
孤城傲影
孤城傲影 2020-12-16 03:06

Currently I get back a JSON response like this...

{items:[
  {itemId:1,isRight:0},
  {itemId:2,isRight:1},
  {itemId:3,isRight:0}
]}

I want

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 03:35

    You could try find the expected result is using the find function, you can see the result in the following script:

    var jsonItems = {items:[
      {itemId:1,isRight:0},
      {itemId:2,isRight:1},
      {itemId:3,isRight:0}
    ]}
    
    var rta =  jsonItems.items.find(
       (it) => {
         return it.isRight === 1;
       }
    );
    
      
    console.log("RTA: " + JSON.stringify(rta));
    
    // RTA: {"itemId":2,"isRight":1}

提交回复
热议问题