简体   繁体   中英

I have an array of objects and an object I want to loop through an object while matching object values with the values in the array

I want to loop through a variable which has ten answers as its values, those ten values must be matched with values on the object so that I can get the strings which are associated with values on answers variable.

var possibleValues = [ { value1:'Completly Agree', weight:5 }, { value1:'Highly Agree', weight:4 }, { value1:'Partialy Agree', weight:3 }, { value1:'Highly Disagree', weight:2 }, { value1:'Completly Disagree', weight:1 } ];

        var answers = [1, 3, 1, 4, 2, 2, 5, 1, 2, 5];
        var result = '';

        /*Loop through answers variable and possibleValues array of objects
         find match between answers value and possibleValues weight
         then if there is a match save value1's value in the result variable*/

        for(i=0;i<answers.length;i++){
            if(possibleValues[i].weight===answers[i].value){
                result = possibleValues[i].value1;
            }
            alert(result);
        }

I want to Loop through answers variable and possibleValues array of objects, find match between answers value and possibleValues weight, then if there is a match save value1's value in the result variable.

THIS IS HOW RESULT VARIABLE MUST LOOK LIKE AFTER ALL result = ['Completly Disagree', 'Partialy Agree', 'Completly Disagree', 'Highly Agree', 'Highly Disagree', 'Highly Disagree', 'Completly Agree', 'Completly Disagree', 'Highly Disagree', 'Completly Agree'];

Try this..

 var possibleValues = [ {value1: "Completly Agree", weight: 5}, {value1: "Highly Agree", weight: 4}, {value1: "Partialy Agree", weight: 3}, {value1: "Highly Disagree", weight: 2}, {value1: "Completly Disagree", weight: 1} ]; var answers = [1, 3, 1, 4, 2, 2, 5, 1, 2, 5]; var result = []; for (var i = 0; i < answers.length; i++) { possibleValues.forEach(function (element) { if (answers[i] == element.weight) { result[i] = element.value1; } }); } console.log(result); 

You could take a Map with all values and weight as keys.

For the wanted result, map the weights by getting the value from the map.

 var possibleValues = [{ value1: 'Completly Agree', weight: 5 }, { value1: 'Highly Agree', weight: 4 }, { value1: 'Partialy Agree', weight: 3 }, { value1: 'Highly Disagree', weight: 2 }, { value1: 'Completly Disagree', weight: 1 }], answers = [1, 3, 1, 4, 2, 2, 5, 1, 2, 5], result = answers.map( Map.prototype.get, possibleValues.reduce((m, { value1, weight }) => m.set(weight, value1), new Map) ); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Just use an object with the values, then map() the array over simply by using the object's values:

 var values = { 5: "Completely Agree", 4: "Highly Agree", 3: "Partially Agree", 2: "Highly Disagree", 1: "Completely Disagree" }; var answers = [1, 3, 1, 4, 2, 2, 5, 1, 2, 5]; var result = answers.map(e => values[e]); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

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