1

Suppose I have json as:

const details = {
    "something1":1,
    "something2":20,
    "something3":123,
    "something4":[{"a":"apple","b":"ball"},
    "something5":1211
}

for this I tried:

let final = {}

for (let keys in details) {
    // console.log(keys)
    if (keys == 'something1'|| 'something2'|| 'something3') {
        final[keys] = details[keys]
    }
}

But it's giving me the same result as details

Expected O/p should be ...

console.log(final) => {"something1":1,"something2":20,"something3":123}
Siva Pradhan
  • 791
  • 1
  • 6
  • 23

4 Answers4

3

You can filter() the entries of the object and then convert it back to object using Object.fromEntries

const details = {
    "something1":1,
    "something2":20,
    "something3":123,
    "something4":[{"a":"apple","b":"ball"}],
    "something5":1211
}

const removeProps = (obj, props) => (
     Object.fromEntries(Object.entries(obj).filter(([k, _]) => !props.includes(k)))
)

const res = removeProps(details, ['something1', 'something2']);
console.log(res)
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
2

the problem is with keys === 'something1'|| 'something2'|| 'something3':

const details = {
    "something1":1,
    "something2":20,
    "something3":123,
    "something4":[{"a":"apple","b":"ball"}],
    "something5":1211,
    }
    
let final = {}

for (let keys in details) {
    // console.log(keys)
    if (keys === 'something1'|| keys === 'something2'|| keys==='something3') {
        final[keys] = details[keys]
    }
}

console.log(final)
Alan Omar
  • 4,023
  • 1
  • 9
  • 20
2

There are already good answers that show how to solve the issue, but they don't explain why your code does not work.

The reason is that you misunderstood what this boolean condition does:

keys == 'something1'|| 'something2'|| 'something3')

It does not mean: key is any of.
It does mean:

  1. if keys == 'something1'
  2. OR if 'something2'
  3. OR if 'something3'

Your 2nd and 3rd or-expressions are just plain strings.
And any non-blank string evaluates to boolean true.

// non-empty strings are boolean TRUE
console.log(`Boolean: 'something2'`, Boolean('something2'));
// empty strings are boolean FALSE
console.log(`Boolean: ''`, Boolean(''));

So your 2nd and 3rd OR conditions are always true and thus the whole condition is true.

So if you want to stick to the explicit comparison, then use this:

if (keys === 'something1'|| keys === 'something2'|| keys==='something3') {

BTW: you may want to use === instead of ==: see Javascript === (triple equals)

TmTron
  • 17,012
  • 10
  • 94
  • 142
1

You could take a function with destructuring and get a new objects.

const
    getParts = ({ something1, something2, something3 }) => ({ something1, something2, something3 }),
    details = { something1: 1, something2: 20, something3: 123, something4: [{ a: "apple", b: "ball" }], something5: 1211 },
    final = getParts(details);

console.log(final);

A dynamic approach

const
    details = { something1: 1, something2: 20, something3: 123, something4: [{ a: "apple", b: "ball" }], something5: 1211 },
    wanted = ['something1', 'something2', 'something3'],
    final = Object.fromEntries(wanted.map(k => [k, details[k]]));

console.log(final);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392