I have JSON object, which I want to sort it in certain way,
This is my object
{ "you": 100, "me": 75, "foo": 116, "bar": 15 }
And I want this object to be sorted in this order ['me', 'foo', 'you', 'bar']
which the object would become like this,
{ "me": 75, "foo": 116, "you": 100, "bar": 15 }
Is there anyway to achieve this ?
You could build a new object by iterating the given array with the keys.
More about order of objects: Does JavaScript Guarantee Object Property Order?
var object = { you: 100, me: 75, foo: 116, bar: 15 }, result = Object.assign(...['me', 'foo', 'you', 'bar'].map(k => ({ [k]: object[k] }))); console.log(result);
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.