Reputation: 15591
I have an object with the following data:
obj =
{
key: 'mykey1597855209',
integrity: 'sha512-T9JW=='
}
{
key: 'mykey159785520915978552101597855212',
integrity: 'sha512-T9JWj=='
}
{
key: 'mykey15978552091597855210',
integrity: 'sha512-lcddfd=='
}
{
key: 'otherkey15978552091597855210',
integrity: 'sha512-abcdfd=='
}
When I do console.log(typeof obj)
, I get object
as output
I want to store only unique integrity
values in an array for the keys which have mykey*
in the key value
Desired Output:
[ 'sha512-T9JWj==', 'sha512-lcddfd==' ]
Code:
var output = [];
for (var key in obj) {
if(obj[key] === 'mykey') {
output.push(obj[integrity])
}
}
console.log(output.join(', '))
Upvotes: 3
Views: 2623
Reputation: 89364
You first filter
the array to obtain only objects with a matching key value, map
each resulting element to its integrity
property, create a Set
from that array, and finally use spread syntax to obtain the result as an array.
const arr = [
{
key: 'mykey1597855209',
integrity: 'sha512-T9JWj=='
},
{
key: 'mykey159785520915978552101597855212',
integrity: 'sha512-T9JWj=='
},
{
key: 'mykey15978552091597855210',
integrity: 'sha512-lcddfd=='
},
{
key: 'otherkey15978552091597855210',
integrity: 'sha512-abcdfd=='
}];
const res = [...new Set(arr.filter(({key})=>/mykey/.test(key)).map(({integrity})=>integrity))];
console.log(res);
Upvotes: 3