根据值对Javascript对象进行排序[复制]

时间:2022-04-01 15:59:42

This question already has an answer here:

这个问题已经有了答案:

I have this json object:

我有这个json对象:

obj = {
    "name":
    {
        "display": "Name",
        "id": "name",
        "position": 3           
    },
    "type":
    {
        "id": "type",
        "position": 0
    },
    "id":
    {
        "display": "ID",
        "id": "id",
        "position": 1
    }
    "key":
    {
        "display": "Key",
        "id": "key",
        "position": 2
    },

}

Is it possible to sort it based on their position numbers, without converting it to an array? I tried to convert it but it changes the format (it deletes the name,type,id,key) and I don't want this.. It should stay as it is (the format) but just in order based on their position.

是否有可能根据它们的位置编号对其进行排序,而不将其转换为数组?我试着转换它,但是它改变了格式(它删除了名称,类型,id,key)我不想要这个。它应该保持原样(格式),但只是根据它们的位置来排序。

what i did:

我所做的:

var array = $.map(obj, function(value, index) {
            return [value];
        });

        array.sort(function(a,b){
            return a.position - b.position;
        });
        obj= JSON.stringify(array);
        obj= JSON.parse(obj);

1 个解决方案

#1


1  

In agreement with the first comments of your question, you should replace your object with an array and remove the redundancies. The position will be determined by that of the element in the array and the keys of your elements will no longer be duplicated with your id properties

与问题的第一个注释一致,您应该用数组替换对象并删除冗余。位置将由数组中的元素决定,元素的键将不再与id属性重复

EDIT: With this code you can sort the object but you can't use id as key because otherwise it will be sorted again alphabetically instead of position.

编辑:使用此代码,您可以对对象进行排序,但不能使用id作为键,否则将按字母顺序而不是位置进行排序。

    let obj = {
    "name":
    {
        "display": "Name",
        "id": "name",
        "position": 3           
    },
    "type":
    {
        "id": "type",
        "position": 0
    },
    "id":
    {
        "display": "ID",
        "id": "id",
        "position": 1
    },
    "key":
    {
        "display": "Key",
        "id": "key",
        "position": 2
    },
    }
    
    let arr = [];
    for(let i in obj) {
    	if(obj.hasOwnProperty(i)) {
    		arr[obj[i].position] = obj[i]
       }
    }
    
    
    let sortedObj = {};
    for(let i in arr) {
    	sortedObj[i] = arr[i]
    }
    
    console.log(sortedObj)

#1


1  

In agreement with the first comments of your question, you should replace your object with an array and remove the redundancies. The position will be determined by that of the element in the array and the keys of your elements will no longer be duplicated with your id properties

与问题的第一个注释一致,您应该用数组替换对象并删除冗余。位置将由数组中的元素决定,元素的键将不再与id属性重复

EDIT: With this code you can sort the object but you can't use id as key because otherwise it will be sorted again alphabetically instead of position.

编辑:使用此代码,您可以对对象进行排序,但不能使用id作为键,否则将按字母顺序而不是位置进行排序。

    let obj = {
    "name":
    {
        "display": "Name",
        "id": "name",
        "position": 3           
    },
    "type":
    {
        "id": "type",
        "position": 0
    },
    "id":
    {
        "display": "ID",
        "id": "id",
        "position": 1
    },
    "key":
    {
        "display": "Key",
        "id": "key",
        "position": 2
    },
    }
    
    let arr = [];
    for(let i in obj) {
    	if(obj.hasOwnProperty(i)) {
    		arr[obj[i].position] = obj[i]
       }
    }
    
    
    let sortedObj = {};
    for(let i in arr) {
    	sortedObj[i] = arr[i]
    }
    
    console.log(sortedObj)