Array path identifier wild card

I have array that I am trying to read values from the problem is part of the path changes in my case it is a mac address. Normal path like say from a mysql return would be msg.payload[0].TalkID but in my case it is payload["be:10:3d:xx:xx:xx"].wds. How can I select the wds field when the path could be difficult with every message.

image

Maybe something like this could be a good starting point?

var keyMatch = function(o,r){

    var c  = 0;
    var nO = {};

    Object.keys(o).forEach(function(k){
        c++;
        no[k] = k.match(r) ? o[k] : void 0;
    });

    return ( ~c ? JSON.stringify(JSON.parse(nO)) : null );
};

var test = {
    '00:02:02:34:72:a5': 'abc' ,
    'Hello world': 'acb',
    '00:02:02:34:72:a3': 'def'
};

keyMatch(test,/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|([0-9a-fA-F]{4}\\.[0-9a-fA-F]{4}\\.[0-9a-fA-F]{4})$/gmi;

you could use object.keys() to find the property name.

msg.payload = msg.payload[Object.keys(msg.payload)[0]].wds
return msg
1 Like