【问题标题】:Create Javascript unsorted array object创建 Javascript 未排序的数组对象
【发布时间】:2015-06-08 10:26:48
【问题描述】:

我想为此创建一个未排序的 JavaScript 数组对象我有两个数组

var degree_values = ['Bachelors', 'Doctoral / PhD', 'Masters', 'MBA', 'Professional Certifications'];
var degree_indexes = ["4", "3", "2", "1", "5"];
var values = {};     
for(var index in degree_values){
    values[degree_indexes[index]] = degree_values[index];
}
console.log(values);

输出是

Object { 1="MBA", 2="Masters", 3="Doctoral / PhD", 4 = 'MBA', 5 = 'Professional Certifications'}

预期的输出是

Object { 4 = 'MBA',  3="Doctoral / PhD", 2="Masters", 1="MBA", 5 = 'Professional Certifications'}

【问题讨论】:

  • 对象不保持其属性的顺序。
  • 你的输出和你预期的输出是一样的,所以一切都很好。
  • 怎么了?您的输出 未排序。
  • 如果您期望答案是未排序的,那么根据定义,您不能期望输出中的顺序。同样,如果您希望结果对象按特定顺序排列,根据定义,您希望它按该顺序排序。你能澄清一下在你使用它的上下文中 unsorted 是什么意思吗?

标签: javascript arrays


【解决方案1】:

轻松使用开源项目jinqJs

See Fiddle

var degree_values = ['Bachelors', 'Doctoral / PhD', 'Masters', 'MBA', 'Professional Certifications'];
var degree_indexes = ["4", "3", "2", "1", "5"];

//Gets emails that are in current not in group
var result = jinqJs().from(degree_values).select(function(row,index){
    var obj = {};
    obj[degree_indexes[index]] = row;

    return obj;
});

【讨论】:

    【解决方案2】:

    尝试将循环更改为for (i = 0; i < degree_values.length; i ++){ 格式,然后使用i 作为数组中的位置访问变量。

    由于您不能依靠浏览器来按顺序维护对象,如How to keep an Javascript object/array ordered while also maintaining key lookups? 的答案所示,您可以构建一个 javascript 对象数组并按照那里的建议进行操作,即为每个对象设置一个 id 和 value元素。

    以下是您的代码进行了这些更改:

    var degree_values = ['Bachelors', 'Doctoral / PhD', 'Masters', 'MBA', 'Professional Certifications'];
    var degree_indexes = ["4", "3", "2", "1", "5"];
    var values = [];     
    for (i = 0; i < degree_values.length; i ++){
        values[i] = {'id': degree_indexes[i], 'value' : degree_values[i]};
    }
    console.log(values);
    

    这是我运行这段代码得到的输出:

    VM1067:12 [Object, Object, Object, Object, Object]
    0: Object
        id: "4"
        value:     "Bachelors"
        __proto__: Object
    1: Object
        id: "3"
        value: "Doctoral / PhD"
        __proto__:     Object
    2: Object
        id: "2"
        value: "Masters"
        __proto__: Object
    3: Object
        id: "1"
        value:     "MBA"
        __proto__: Object
    4: Object
        id: "5"
        value: "Professional Certifications"
        __proto__: Object
    length: 5
    __proto__: Array[0]
    undefined
    

    【讨论】:

    • “这将有助于维持连接/秩序”——唯一的“问题”是没有维持秩序。只需尝试应用您的建议运行 OP 的代码并查看它。
    • eval?严重地?为什么不直接创建一个对象?
    • @zerkms 感谢您的反馈。我按照建议编辑了答案。
    【解决方案3】:
    Your output is: Object { 1="MBA", 2="Masters", 3="Doctoral / PhD", 4 = 'Bachelors', 5 = 'Professional Certifications'}
    Expected : Object { 4 = 'MBA',  3="Masters", 2="Doctoral / PhD", 1="Bachelors", 5 = 'Professional Certifications'}
    

    由于您的键是数字,因此浏览器会按顺序显示它,因此您最终想要得到的是(相同但按顺序排列):

    Object { 1: "Bachelors", 2: "Doctoral / PhD", 3: "Masters", 4: "MBA", 5: "Professional Certifications" }
    

    在您的 for 中更改值而不是键(我刚刚评论了您的行):

    var degree_values = ['Bachelors', 'Doctoral / PhD', 'Masters', 'MBA', 'Professional Certifications'];
    var degree_indexes = ["4", "3", "2", "1", "5"];
    var values = {};
    
    for(var index in degree_values) {
       // values[degree_indexes[index]] = degree_values[index];
       values[degree_indexes[index]] = degree_values[degree_indexes[index]-1]
    }
    

    现在您的对象中有所需的键和值。然后按degree_indexes 的顺序显示属性,我使用了一个在其原型中链接的高阶函数:

    degree_indexes.forEach(elm => {
       console.log(degree_values[elm-1]) /* 4, 3, 2, 1, 0*/
    })
    
    Output:
       MBA
       Masters
       Doctoral / PhD
       Bachelors
       Professional Certifications
    

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 2021-06-01
      • 2018-05-15
      • 2012-12-10
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2016-04-06
      相关资源
      最近更新 更多