I've a problem: i would like to move a key/value of an object by one position, I'm using Javascript, i could also use Lodash, but i cannot transform the object into an array.
My object
"input": {
"10_": "ab",
"20_": "cd",
"30_": "ef"
}
Result
"input": {
"20_": "cd",
"10_": "ab",
"30_": "ef"
}
Thank you.
I've a problem: i would like to move a key/value of an object by one position, I'm using Javascript, i could also use Lodash, but i cannot transform the object into an array.
My object
"input": {
"10_": "ab",
"20_": "cd",
"30_": "ef"
}
Result
"input": {
"20_": "cd",
"10_": "ab",
"30_": "ef"
}
Thank you.
Share Improve this question asked Sep 6, 2019 at 7:12 rikg93rikg93 1,2891 gold badge12 silver badges22 bronze badges 12- 1 in this case, i suggest to use an array with the ordered key for access. i would not rely on the order of properties. Does JavaScript Guarantee Object Property Order? – Nina Scholz Commented Sep 6, 2019 at 7:16
- If you need an ordered object, use an array. Though ES2015 specifies some ordering for object's properties, that can't be used like this. – Teemu Commented Sep 6, 2019 at 7:16
- you can also use Set which preserves order. xahlee.info/js/javascript_set.html – Harpreet Singh Commented Sep 6, 2019 at 7:17
- whats the pattern to change the values? – Ajay Kumar Oad Commented Sep 6, 2019 at 7:17
- 1 Making sense is not task dependent, it's language dependent. An object is like a sack where you drop the properties in. And when you read the properties, they are lifted from the sack one by one more or less randomly until the sack is empty. ES2015 Set and Map are only making a hole to the bottom of the sack, and reading the properties in the order you've originally dropped them in. But, in the meanwhile, you've shaken the sack. – Teemu Commented Sep 6, 2019 at 7:41
3 Answers
Reset to default 1In javascript object property order is equivalent to insertion order for string keys and ascending order for number keys, that being the case you can simply insert them in an order you want to obtain a desired result
const input = {
"10_": "ab",
"20_": "cd",
"30_": "ef"
}
console.log({"20_": input["20_"],"10_": input["10_"],"30_": input["30_"]})
or slightly more general
const input = {
"10_": "ab",
"20_": "cd",
"30_": "ef",
"40_": "ef",
"50_": "ef"
}
const [firstKey, secondKey] = Object.keys(input);
const {[firstKey]: firstKeyValue, [secondKey]: secondKeyValue, ...rest} = input
console.log({[secondKey]: secondKeyValue, [firstKey]: firstKeyValue, ...rest })
An object is unlike an array, the order does not matter.
When retrieving data from an array, you might need to iterate through the entire array. But for an object that has key-value pairs, you just need to access the value using the key.
For example, to access the value of _10 (below), you just have to write fruits.a
or fruits['a']
const fruits = {
'a': 'apple' ,
'b': 'banana'
};
const fruits2 = {
'b': 'banana',
'a': 'apple'
};
JavaScript treats both objects the same way and you get the same output when you do fruits.a
and fruits2.a
Positioning of object values have no impact in the Object and how it's properties and methods called. The object properties and methods are called and set only by their keys.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745217282a4617085.html
评论列表(0条)