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(', '))
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.