0

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' ];

4 Answers4

1

Since arrayLegends is containing the keys you want to retrieve in legendsObj in the right order, you only have to iterate through arrayLegends and push the corresponding values in legendsValue:

var arrayLegends = ["totalregisteredfirst","totalregisteredsecond", "totalregisteredfourth"];

var lengendsObj = {totalregisteredfirst: "1st visit", totalregisteredfourth: "4th visit", totalregisteredsecond: "2nd visit"}

var legendsValue = [];

arrayLegends.forEach(function(value) {
  legendsValue.push(lengendsObj[value]);
});
  
console.log('legends value', legendsValue);
remix23
  • 2,261
  • 1
  • 8
  • 18
1

Check this out :

let arrayLegends = ["totalregisteredfirst","totalregisteredsecond", "totalregisteredfourth"];

let lengendsObj = {totalregisteredfirst: "1st visit", totalregisteredfourth: "4th visit", totalregisteredsecond: "2nd visit"}

var legendsValue = arrayLegends.map((key) => lengendsObj[key]);
Arpit Agrawal
  • 950
  • 8
  • 18
0

Your code is really close to getting your desired result, you just need use your arrayLegends as the outer loop and your Object.entries(lengendsObj) for your inner loop:

var arrayLegends = ["totalregisteredfirst","totalregisteredsecond", "totalregisteredfourth"];

var lengendsObj = {totalregisteredfirst: "1st visit", totalregisteredfourth: "4th visit", totalregisteredsecond: "2nd visit"}

var legendsValue = [];

arrayLegends.forEach(function (value) {
 Object.entries(lengendsObj).forEach(function([objKey, objValue]) {
  if (value === objKey ) {
    legendsValue.push(objValue);
  }
 });
});

console.log('legends value', legendsValue);
matthias.rocks
  • 585
  • 2
  • 7
  • Why iterate over the legendsObj when you can directly get the value from object since you already have the key? – Arpit Agrawal Feb 14 '19 at 11:45
  • @ArpitAgrawal I wanted to show OP how close he was to get his desired result. But for sure, the runtime can be greatly improved from the n^2 operation in mine and OP's posts. Your answer is really good in this regard. – matthias.rocks Feb 14 '19 at 11:53
0

Object order is not guaranteed in JavaScript. Iterating over an object will not necessarily be done in the same order on independent runs. Iterate over your array instead; that does have guaranteed order.

var arrayLegends = ["totalregisteredfirst","totalregisteredsecond", "totalregisteredfourth"];

var legendsObj = {totalregisteredfirst: "1st visit", totalregisteredfourth: "4th visit", totalregisteredsecond: "2nd visit"}

var legendsValue = [];

arrayLegends.forEach(k => {
    legendsValue.push(legendsObj[k]);
});
Effective Robot
  • 206
  • 1
  • 5