1

I have an object:

var obj = { a: 'test1', b: 'test2', c: 'test3', d: 'test4', e: 'test5', f: 'test6', g: 'test7', h: 'test8' }

I want to get result:

res = { a: 'test1', c: 'test3', d: 'test4' }

What is the fastest way to do it?

8
  • use delete obj.e Commented Oct 19, 2018 at 8:03
  • @Justcode Hmmm, you might want to test that. Particularly test accessing other properties on obj after you do it. :-) Commented Oct 19, 2018 at 8:03
  • @T.J.Crowder shouldn't be a problem with if you are dealing with 1 property. Commented Oct 19, 2018 at 8:40
  • 1
    @T.J.Crowder thank you, time to do some re-work :D Commented Oct 19, 2018 at 9:46
  • 1
    @Justcode :-) Of course, it's a bit micro-opt. (And that synthetic benchmark doesn't reveal the problem on Firefox; the delete right after creation gets optimized away I think.) It's just, the OP did specifically say "fastest"... ;-) Commented Oct 19, 2018 at 10:02

2 Answers 2

9

Directly access the fields:

const res = {a: obj.a, c: obj.c, d: obj.d};

Live Example:

const obj = {
    a: "test1",
    b: "test2",
    c: "test3",
    d: "test4",
    e: "test5",
    f: "test6",
    g: "test7",
    h: "test8",
};
const res = { a: obj.a, c: obj.c, d: obj.d };
console.log(JSON.stringify(res, null, 4));


In a comment, Himanshu Agrawal asked:

What if the key is unknown and stored in a variable? const keys = ["a", "c", "d"];

I'd probably use a for-of loop to handle that:

const res = {};
for (const key of keys) {
    res[key] = obj[key];
}

Live Example:

const obj = {
    a: "test1",
    b: "test2",
    c: "test3",
    d: "test4",
    e: "test5",
    f: "test6",
    g: "test7",
    h: "test8",
};
const keys = ["a", "b", "c"];
const res = {};
for (const key of keys) {
    res[key] = obj[key];
}
console.log(JSON.stringify(res, null, 4));

But you could also use map and Object.fromEntries:

const res = Object.fromEntries(keys.map((key) => [key, obj[key]]));

Live Example:

const obj = {
    a: "test1",
    b: "test2",
    c: "test3",
    d: "test4",
    e: "test5",
    f: "test6",
    g: "test7",
    h: "test8",
};
const keys = ["a", "b", "c"];
const res = Object.fromEntries(keys.map((key) => [key, obj[key]]));
console.log(JSON.stringify(res, null, 4));

That said, the question asks for the fastest way to do it, and the map+Object.fromEntries approach involves several temporary object allocations and function calls. In most cases, it won't matter but the for-of is probably faster (depending on the degree of optimization the JavaScript engine does). Or a boring old-fashioned for loop might be faster still:

const res = {};
for (let n = 0; n < keys.length; ++n) {
    const key = keys[n];
    res[key] = obj[key];
}

Live Example:

const obj = {
    a: "test1",
    b: "test2",
    c: "test3",
    d: "test4",
    e: "test5",
    f: "test6",
    g: "test7",
    h: "test8",
};
const res = {};
for (let n = 0; n < keys.length; ++n) {
    const key = keys[n];
    res[key] = obj[key];
}
console.log(JSON.stringify(res, null, 4));

Again, it's unlikely to matter, but it's good to have multiple approaches for situations where it may.

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

2 Comments

What if the key is unknown and stored in a variable? const keys = ["a", "c", "d"];
@HimanshuAgrawal - I've updated the post to answer that additional question.
1

i think you want to delete key-value pair from the object so for that here's the solution

delete obj[b];

delete obj[e];

or you can use lodash pick

var _ = require('lodash')
_.pick( obj, [a, c, d] )

or create a new Object

var final = {a: obj.a, c: obj.c, d: obj.d}

3 Comments

You might want to test that. Particularly test accessing other properties on obj after you do it. :-) delete has a significant unfortunate affect on the performance of objects you apply it to.
yeah, but i've seen a lot of cases where one need to change the current variable only instead of creating a new one. That's why i've multiple methods. Anyway thanks for pointing that out. :)
Well, you didn't when I posted that comment. And _.pick is certainly not going to be the "fastest" way (not even remotely).

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.