let testArray = [{"length":"None","duration":"10000", "percentage":"65"},
{"width":"Half","detail":"under","duration":"25000","percentage":"25"},
{"length":"Full","duration":"20000","percentage":"90"}];
// Reference the element that'll display results
const test = document.querySelector('.test');
/**
* @desc - Given an array of objects, a key and a value, generate a <mark>
* with an index of the current object for each key/value pair.
* If a pair matches the 2nd and 3rd @param the index will be red.
* @param {array<object>} array - An array of objects
* @param {string} key - The first string of a pair representing a property
* name
* @param {string} val - The second string of a pair representing a value
* @param {object<DOM>} node - The DOM Object that the results will be
* displayed in @default is <body>
*/
function findKV(array, key, val, node = document.body) {
// Prepare key/val for comparison -- matching is case insensitive
key = key.toLowerCase();
val = val.toLowerCase();
// For each object of the array...
array.forEach((obj, idx) => {
/*
Convert object into an array of pairs -- pass the destructed
[key, value] array pair
*/
Object.entries(obj).forEach(([k, v]) => {
/*
Compare with a ternary:
If key equals k AND val equals v color is red -- otherwise it's blue
*/
let color = key === k.toLowerCase() && val === v.toLowerCase() ?
'red' : 'blue';
/*
Generate a <mark> with an index of current object with the
appropriate color
*/
node.innerHTML +=
`<mark style='color:${color};font-size:4rem;'>
${idx}
</mark>`;
});
});
// Add a line-break
node.innerHTML +=`<br>`;
}
findKV(testArray, 'length', 'None', test);
findKV(testArray, 'percentage', '25', test);
findKV(testArray, 'duration', '20000', test);
html {font: 400 1ch/1 Consolas}
.test {outline: 4px dashed brown}
<div class='test'></div>