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
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
If you want to get first characteristic of each service uuid