[英]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. 我想遍历一个具有十个答案作为其值的变量,这十个值必须与对象上的值匹配,以便我可以获取与Answers变量上的值相关联的字符串。
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可能的值= [{value1:'完全同意',weight:5},{value1:'高度同意',weight:4},{value1:'Partialy Agree',weight:3},{value1:'高度不同意' ,weight:2},{value1:'完全不同意',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. 我想循环遍历答案变量和对象的valuesvalues数组,找到答案值和valuesvalues权重之间的匹配,然后在结果变量中是否存在匹配保存value1的值。
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. 您可以将所有值和
weight
作为键的Map
。
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: 只需使用带有值的对象,然后只需使用对象的值即可将数组
map()
覆盖:
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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.