Hello, I have an object that contains an array of objects, and I would like to check inside every object of the array if the item “json_formatted” exists before doing operations.
My code:
var obj1 = { model: "500", color: "white", constructor: { skill: { json_formatted: "{\"irregular\":{\"pulse_quality\":[255,0],\"pulse\":[91,0]}}" } } };
var obj2 = { model: "200", color: "black", constructor: { skill: { json_formatted: "{\"irregular\":{\"pulse_quality\":[255,0,255,500,255,1001,255,1504,255,2007,255,2512,255,3015,255,3517,255,4018,255,4521,255,5024,255,5530,255,6033,255,6534,255,7036,255,7539,255,8043,255,8547,255,9049,255,13208,255,13741,255,14243,255,14745,255,15284,255,15827,255,16367,255,16910,255,17450,255,17991,255,18533,255,19075,255,19616,255,20157,255,20659,255,21199,255,21742,255,22282,255,22823,255,23366,255,23909,255,24449,255,24990,255,25532,255,26073,255,26616,255,27156,255,27657,255,28158,255,28699,255,29239,255,29782,255,30322,255,30865,255,31366,255,31905,255,32448,255,32949,255,33488,255,34031,255,34571,255,35115,255,35654,255,36155,255,36698,255,37237,255,37781,255,38320,255,38864,255,39403,255,39947,255,40487,255,41030,255,41570,255,42114,255,42653,255,43197,255,43736,255,44280,255,44819,255,45363,255,45902,255,46446,255,46984,255,47485,255,48029,255,48568,255,49113,255,49651,255,50195,255,50734,255,51279,255,51817,255,52360,255,52862,255,53400,255,53945,255,54483,255,54984,255,55527,255,56066,255,56612,255,57149,255,57694,255,58194,255,58733,255,59277,255,59816],\"pulse\":[77,0,77,500,77,1001,77,1504,77,2007,77,2512,77,3015,77,3517,77,4018,77,4521,77,5024,77,5530,77,6033,77,6534,77,7036,77,7539,77,8043,77,8547,74,9049,74,13208,74,13741,74,14243,74,14745,79,15284,79,15827,79,16367,79,16910,79,17450,79,17991,79,18533,79,19075,79,19616,79,20157,81,20659,81,21199,81,21742,81,22282,81,22823,78,23366,78,23909,78,24449,78,24990,78,25532,80,26073,80,26616,80,27156,80,27657,80,28158,80,28699,80,29239,80,29782,80,30322,80,30865,83,31366,83,31905,83,32448,83,32949,83,33488,83,34031,82,34571,82,35115,82,35654,82,36155,82,36698,82,37237,82,37781,82,38320,82,38864,82,39403,82,39947,82,40487,82,41030,82,41570,82,42114,82,42653,82,43197,82,43736,82,44280,82,44819,82,45363,82,45902,82,46446,82,46984,82,47485,82,48029,82,48568,82,49113,82,49651,82,50195,82,50734,82,51279,82,51817,82,52360,82,52862,82,53400,82,53945,82,54483,82,54984,82,55527,82,56066,82,56612,82,57149,82,57694,82,58194,82,58733,82,59277,82,59816]}}" } } };
var obj3 = { model: "600", color: "orange", constructor: { skill: {} } };
var obj4 = { model: "100", color: "red", constructor: { skill: { json_formatted: "{\"irregular\":{\"pulse_quality\":[255,0],\"pulse\":[21,0]}}" } } };
var obj5 = { model: "900", color: "blue", constructor: { skill: { json_formatted: "{\"irregular\":{\"pulse_quality\":[255,0],\"pulse\":[99,0]}}" } } };
var multiobject = {obj1, obj2, obj3, obj4, obj5};
var edge = [];
Object.keys(multiobject).map(function(objectKey, index) {
var value = multiobject[objectKey].constructor.skill.json_formatted;
var ex = JSON.parse(value);
edge.push(ex);
});
edge.forEach((e) => {
e.irregular.pulse = e.irregular.pulse.filter(f => f % 2 == 0);
e.irregular.pulse_quality = e.irregular.pulse_quality.filter(f => f % 2 == 1);
})
The code actually returns an error because obj3 has no json_formatted field. I would like to check in the .map operation if the field exists before doing the operations to prevent this error from happening and only continue with the objects that have the json_formatted field.
Thanks.