Comments (8)
Hi Tomek! :)
Keys order on object is not guaranteed by specification, and you should never rely on it.
So even if you try to assign properties in specific order, it's not good idea to assume that reiteration will work in same order.
If I didn't get your reasoning, please explain further.
I'll close it for now, but let's keep discussion here.
from es5-ext.
Hello Mariusz :)
Of course I know that ordering of properties in object is not guaranteed by default, but quite common practice is to add order
sub-property to each property of an object what can be then used for ordering. So the object could look like that:
var object = {
propertyOne: { value: "One", order: 0 },
propertyTwo: { value: "Two", order: 1 }
};
In my case I have several objects with order
sub-property and I want to merge them into one object but preserving ordering of each of them. So my code looks like that:
extend(compoundObject, mapKeys(objectOne, function (name, value) {
value.order = ++index;
return name;
}, null, byOrder));
Of course in that case byOrder
function will be ignored and sub-properties order
for all properties from objectOne
will be assigned with new value in non-deterministic order. The working code I use right now is:
forEach(objectOne, function (value, name) {
value.order = ++index;
compoundObject[name] = value;
}, null, byOrder);
Maybe the difference is not big, but I think that sometimes first approach with extend
and mapKeys
would be preferred over forEach
. What do you think?
Kind regards,
Tomasz
from es5-ext.
Tomek, thanks, still I have trouble understanding the example.. Is this a case, where there are holes in order properties e.g. it's 1,3,5,6 and you want to fix it so it's 1,2,3,4?
As otherwise setting value.order = ++index
doesn't make much sense, as instead you could do value.order = value.order
but that also doesn't seem to make much sense :)
from es5-ext.
I placed only a fragment of code and that could make the example vague. I have more objects which I want to merge into single compoundObject
and in each one properties are ordered from 1 to n. Because of that in compoundObject
I need to increase order
of properties taken from second and third object and so on. Otherwise I would have multiple properties (as many as number of objects to merge) with order 1 and 2 and so on. More explanatory example would be:
var index = 0;
extend(compoundObject, mapKeys(objectOne, function (name, value) {
value.order = ++index;
return name;
}, null, byOrder));
extend(compoundObject, mapKeys(objectTwo, function (name, value) {
value.order = ++index;
return name;
}, null, byOrder));
extend(compoundObject, mapKeys(objectThree, function (name, value) {
value.order = ++index;
return name;
}, null, byOrder));
I hope now my use case is clear. Of course the question whether extend
and mapKeys
is better than forEach
is still open and I would be glad to know your opinion on that subject :)
from es5-ext.
Ok, few more questions :)
Why mapKeys
? In above snippet you just reuse same name, do you change property names in original code?
Other weird thing, is that by reassigning order, you're changing order not only for compoundObject
but also for objectOne
, objectTwo
and objectThree
properties, are they temporary objects that are discarded (no longer used) afterwards?
from es5-ext.
Yeah, probably I should place original code.. I will send it to you as I don't want to share it in the Internet.
Of course I change property names, otherwise I agree using mapKeys
would be simply stupid :) I am also aware that I am changing original objects, but this is not a problem since in my code they are actually copies of original objects:
var objectOne = copy(originalObjectOne);
from es5-ext.
@bsodzik now it's clear, it's my fault! :P I totally overdone that piece then. I hope my email makes that clear, just use forEach
seems simpler and more readable.
from es5-ext.
@medikoo yes, now everything is clear for me as well. I have to agree with you that with forEach
code looks more readable so I will forget about mapKeys
in this particular case. Thank you very much for great support! :)
from es5-ext.
Related Issues (20)
- v0.10.54 release breaks package installation HOT 25
- The NPM package is not a place for politics HOT 7
- Reasoning for anti-war manifest HOT 609
- On manifestations :) HOT 6
- Perhaps, some improvements to wording HOT 4
- Unfortunately the protest-ware in the manifest means your package has been marked as critical HOT 2
- Postinstall broken for pnpm 7+ HOT 6
- Postinstall not fit for Windows HOT 23
- This is not malware, but... HOT 1
- package being detected as a virus HOT 47
- npm ERR! code EINTEGRITY HOT 2
- Violation of Security-Malicious HOT 8
- Error: ELOOP: too many symbolic links encountered HOT 2
- fu'ed postinstall message HOT 2
- Clean repo from Malware scripts HOT 1
- Move "call for peace" to `README.md` to avoid unintended consequences HOT 1
- redos in es5-ext HOT 24
- Why you don't call for peace in Gaza? HOT 1
- Failed to exec postinstall script on Linux HOT 5
- Can't install on linux HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from es5-ext.