简体   繁体   中英

How to particular key's value in an object based on the values from another array and using arrays sequence in mind

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

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); 

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

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); 

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]);
});

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM