1

What is best approach in extracting the below object. I tried two ways. Can this be refactored in any other way to optimize the performance?

const data = {
    "postcode": [
        "123456"
    ],
    "time": [
        "05:11"
    ]
};

Approach 1:

for (const element of Object.entries(data)) {
    const key = element[0];
    const value = element[1][0];
    data[key] = value;
}

Approach 2:

for (const key in data) {
    if (Object.hasOwnProperty.call(data, key)) {
        data[key] = data[key][0];
    }
}

console.log('data ', data);
8
  • 1
    What do you mean 'extract the below object'? Are there constraints on what the object's contents will be (i.e. only one element in the array)? What's the expected output? Commented Jun 10, 2022 at 6:30
  • Performance of your code looks pretty good as is. You can use in-built methods like Object.fromEntries() and .map() to make your code more concise, but if you're after performance improvements I don't think much can be done, other than some potential micro-optimizations that won't make much of a difference in the grand scheme of things. Commented Jun 10, 2022 at 6:32
  • @NickParsons Can you post an example with map and fromEntries? Commented Jun 10, 2022 at 6:33
  • 2
    @kittu it would be something like this const res = Object.fromEntries(Object.entries(data).map(([key, [val]]) => [key, val]));, adding this as a comment as this doesn't answer your question of optimizing your code (it would make your code slightly slower actually), it's just more concise I guess Commented Jun 10, 2022 at 6:36
  • 2
    @mousetail In your answer you've refactored OPs first code-block, which I think does answer the refactoring part of the question, and your example doesn't really lower the performance of the existing code, so I think your answer is fine :). I do think OPs second approach of for (const key in data) { would be the most efficient (assuming the prototype isn't filled with enumerable properties), as it is only one iteration of the object, whereas .entries() + for..of leads to two iterations over the keys/values. Commented Jun 10, 2022 at 6:55

2 Answers 2

3

A concise method is this:

for (const [key, [value]] of Object.entries(data)) {
    data[key] = value
}

Notice the extra set of [] around [value] to extract the value from that array immediately too.

Of course, conciseness isn't always better and you may prefer your other solutions if you think they are more readable.

In terms of performance this isn't any better than your first solution. Your second option will be more memory efficient though it heavily depends on the size and shape of your object.

Sign up to request clarification or add additional context in comments.

Comments

0

This approach will solve your problem. MDN recommended!

const object1 = {
  a: 'somestring',
  b: 42
};

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

// expected output:
// "a: somestring"
// "b: 42"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.