I have an object with its key-value pair and based on the value from another array which is having only key which match the objects key. i wanted to form a different array. keeping in mind the sequence of the array and same way i want to make another array with only values of the object.
i have tried with object.entries and using forEach to key through the object but unable to get the desired results.
var arrayLegends = ["totalregisteredfirst","totalregisteredsecond", "totalregisteredfourth"];
var lengendsObj = {totalregisteredfirst: "1st visit", totalregisteredfourth: "4th visit", totalregisteredsecond: "2nd visit"}
var legendsValue = [];
Object.entries(lengendsObj).forEach(function([objKey, objValue]) {
arrayLegends.forEach(function(value) {
if (value === objKey ) {
legendsValue.push(objValue);
}
});
});
console.log('legends value', legendsValue);
Expected Resultant Array = [ '1st visit', '2nd visit', '4th visit' ];
Actual Resultant Array = [ '1st visit', '4th visit', '2nd visit' ];