skip to Main Content

I am using this code to filter property value which has true. Filter value may get array of objects. But I need to take first characteristic.

var filter = Service.filter(s => s.characteristics.find(c => c.properties?.write == true));

console.log(JSON.stringify(Service));
console.log(filter); // May get 1 or more characteristics

console.log(filter[0].uuid + ";;;" + filter[0].characteristics[0].uuid);

Here is a demo with the data

var Service = [{
    "characteristics": [{
        "descriptors": [],
        "uuid": "2a00",
        "properties": {
          "write": false,
          "writeWithoutResponse": true,
          "read": true
        }
      },
      {
        "descriptors": [],
        "uuid": "2a01",
        "properties": {
          "read": true
        }
      },
      {
        "descriptors": [],
        "uuid": "2a02",
        "properties": {
          "read": true
        }
      },
      {
        "descriptors": [],
        "uuid": "2a03",
        "properties": {
          "write": false
        }
      },
      {
        "descriptors": [],
        "uuid": "2a04",
        "properties": {
          "read": true
        }
      }
    ],
    "uuid": "1800"
  },
  {
    "characteristics": [{
      "descriptors": [{
        "uuid": "2902"
      }],
      "uuid": "2a05",
      "properties": {
        "indicate": true
      }
    }],
    "uuid": "1801"
  },
  {
    "characteristics": [{
        "descriptors": [{
          "uuid": "2902"
        }],
        "uuid": "2a37",
        "properties": {
          "notify": true
        }
      },
      {
        "descriptors": [],
        "uuid": "2a38",
        "properties": {
          "read": true
        }
      }
    ],
    "uuid": "180d"
  },
  {
    "characteristics": [{
        "descriptors": [],
        "uuid": "2a23",
        "properties": {
          "read": true
        }
      },
      {
        "descriptors": [],
        "uuid": "2a24",
        "properties": {
          "read": true
        }
      },
      {
        "descriptors": [],
        "uuid": "2a25",
        "properties": {
          "read": true
        }
      },
      {
        "descriptors": [],
        "uuid": "2a26",
        "properties": {
          "read": true
        }
      },
      {
        "descriptors": [],
        "uuid": "2a27",
        "properties": {
          "read": true
        }
      },
      {
        "descriptors": [],
        "uuid": "2a28",
        "properties": {
          "read": true
        }
      },
      {
        "descriptors": [],
        "uuid": "2a29",
        "properties": {
          "read": true
        }
      }
    ],
    "uuid": "180a"
  },
  {
    "characteristics": [{
      "descriptors": [],
      "uuid": "2a19",
      "properties": {
        "read": true
      }
    }],
    "uuid": "180f"
  },
  {
    "characteristics": [{
        "descriptors": [],
        "uuid": "WrongID",
        "properties": {
          "read": true
        }
      },
      {
        "descriptors": [{
          "uuid": "2902"
        }],
        "uuid": "characteruuid",
        "properties": {
          "write": true,
          "indicate": true
        }
      }
    ],
    "uuid": "serviceuuid"
  }
];



var filter = Service.filter(s => s.characteristics.find(c => c.properties?.write == true));

console.log(JSON.stringify(Service));
console.log(filter); // May get 1 or more characteristics

console.log(filter[0].uuid + ";;;" + filter[0].characteristics[0].uuid);
Expected Output : serviceuuid ::: characteruuid
Current         : serviceuuid ::: WrongID

3

Answers


  1. var Service =[{
         
          "characteristics": [
            {
              "descriptors": [
    
              ],
              "uuid": "2a00", 
              "properties": {
                "write": false,
                "writeWithoutResponse": true,
                "read": true
              }
            },
            {
              "descriptors": [
    
              ],
              "uuid": "2a01", 
              "properties": {
                "read": true
              }
            },
            {
              "descriptors": [
    
              ],
              "uuid": "2a02", 
              "properties": {
                "read": true
              }
            },
            {
              "descriptors": [
    
              ],
              "uuid": "2a03", 
              "properties": {
                "write": false
              }
            },
            {
              "descriptors": [
    
              ],
              "uuid": "2a04", 
              "properties": {
                "read": true
              }
            }
          ],
          "uuid": "1800" 
        },
        {
          "characteristics": [
            {
              "descriptors": [
                {
                  "uuid": "2902"
                }
              ],
              "uuid": "2a05", 
              "properties": {
                "indicate": true
              }
            }
          ],
          "uuid": "1801"
        },
        {
          "characteristics": [
            {
              "descriptors": [
                {
                  "uuid": "2902"
                }
              ],
              "uuid": "2a37",
              "properties": {
                "notify": true
              }
            },
            {
              "descriptors": [
    
              ],
              "uuid": "2a38",
              "properties": {
                "read": true
              }
            }
          ],
          "uuid": "180d" 
        },
        {
          "characteristics": [
            {
              "descriptors": [
    
              ],
              "uuid": "2a23", 
              "properties": {
                "read": true
              }
            },
            {
              "descriptors": [
    
              ],
              "uuid": "2a24", 
              "properties": {
                "read": true
              }
            },
            {
              "descriptors": [
    
              ],
              "uuid": "2a25", 
              "properties": {
                "read": true
              }
            },
            {
              "descriptors": [
    
              ],
              "uuid": "2a26",
              "properties": {
                "read": true
              }
            },
            {
              "descriptors": [
    
              ],
              "uuid": "2a27", 
              "properties": {
                "read": true
              }
            },
            {
              "descriptors": [
    
              ],
              "uuid": "2a28",
              "properties": {
                "read": true
              }
            },
            {
              "descriptors": [
    
              ],
              "uuid": "2a29", 
              "properties": {
                "read": true
              }
            }
          ],
          "uuid": "180a" 
        },
        {
          "characteristics": [
            {
              "descriptors": [
    
              ],
              "uuid": "2a19", 
              "properties": {
                "read": true
              }
            }
          ],
          "uuid": "180f" 
        },
        {
          "characteristics": [
            {
              "descriptors": [
    
              ],
              "uuid": "WrongID",
              "properties": {
                "read": true
              }
            },
            {
              "descriptors": [
                {
                  "uuid": "2902"
                }
              ],
              "uuid": "characteruuid",
              "properties": {
                "write": true,
                "indicate": true
              }
            }
          ],
          "uuid": "serviceuuid"
        }
      ];
    
    var filter = Service.find(service =>
      service.characteristics.some(characteristic => characteristic.properties?.write === true)
    );
    
    if (filter) {
      var firstCharacteristic = filter.characteristics.find(characteristic => characteristic.properties?.write === true);
      console.log(filter.uuid + " ::: " + firstCharacteristic.uuid);
    } else {
      console.log("No matching service found.");
    }
    
    //you can also try this solution 
    
    /*
    
    const filter = Service.find(service => {
      const firstCharacteristic = service.characteristics.find(
    characteristic => characteristic.properties?.write === true
      );
      if (firstCharacteristic) {
    console.log(service.uuid + " ::: " + firstCharacteristic.uuid);
    return true; // Stop searching after the first match
      }
      return false; // Continue searching
    });
    
    if (!filter) {
      console.log("No matching service found.");
    }
    
    */
    Login or Signup to reply.
  2. var filter=Service.find(s=>s.characteristics.some(c=>c.properties?.write==true));
    

    console.log ( JSON.stringify(Service));
    console.log(filter); // May get 1 or more characteristics

    console.log(filter[0].uuid +";;;" + filter[0].characteristics[0].uuid);

    Expected Output : serviceuuid ::: characteruuid
    Current : serviceuuid ::: WrongID

    Login or Signup to reply.
  3. If you want to get first characteristic of each service uuid

    let stats = {};
    for (const service of Service) {
      const { uuid, characteristics } = service;
      const characteristicFound = characteristics.find(
        characteristic => characteristic?.properties?.write === true
      );
      stats[uuid] = characteristicFound?.uuid;
    }
    
    console.log(stats);
    // result: 
    {
      '1800': null,
      '1801': null,
      '180d': null,
      '180a': null,
      '180f': null,
      serviceuuid: 'characteruuid'
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search