我有一个对象数组和一个对象,我想遍历对象,同时将对象值与数组中的值匹配

时间:2019-01-09 10:15:55

标签: javascript

我想遍历一个具有十个答案作为其值的变量,这十个值必须与对象上的值匹配,以便我可以获取与Answers变量中的值相关联的字符串。

variableValues = [             {                 值1:“完全同意”,                 重量:5             },             {                 值1:“高度同意”,                 重量:4             },             {                 值1:“部分同意”,                 重量:3             },             {                 值1:“高度不同意”,                 重量:2             },             {                 值1:“完全不同意”,                 重量: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);
        }

我想遍历答案变量和对象的valuesvalues数组,找到答案值和valuesvalues权重之间的匹配,然后在结果变量中是否存在匹配值保存value1的值。

这就是所有之后看起来如何的结果变量 结果= [“完全不同意”,“部分同意”,“完全不同意”,“高度同意”,“高度不同意”,“高度不同意”,“完全同意”,“完全不同意”,“高度不同意”,“完全不同意”同意'];

3 个答案:

答案 0 :(得分:0)

尝试一下。

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

答案 1 :(得分:0)

您可以将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; }
   getMethod () {
            AXIOS.get(`/url/`)

                .then(response => {
                        if (response.status == 200) {

this.response = response.data                                     
                            this.myTest = response.data.map(mesg => mesg.carManufacterId)
                            console.log(this.myTest)

                        }
                    },
                    (err) => {                

                       if (err.response.status == 500) {

                            console.log('turned off')
                        }
                        else if (err.response.status == 404) {                         
                                console.log('Could not retrieve)
                        }
                    })

        },

答案 2 :(得分:0)

只需使用带有值的对象,然后简单地通过使用对象的值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;
}