1

Given an Object s as

s = {
   a:1,
   b:2
}

I can define a new object t with fields depending on the contents of s,

say something like

t = {
   d: s.a ? 1 : 2
}

then the field d value depends on s.a ( Value of d depends on whether s.a is defined or not).

How can I exclude a field in an object depending o the value of s? , Something like

t = {
   d: s.a ? 1 : undefined
}

This doesn't work though ...

I know this can be done with a couple of if else but I'm looking for an elegant solution/ oneliners

My object is pretty huge, so I do not want to do something like

t = s.a? {
       d: 1 
    } : {}

EDIT I've seen a wide variety of solutions, I'm looking at a solution that is a oneliner/has minimum changes and is readable. Something like an idiomatic javascript/ecmascript 6

EDIT The duplicate found here seems to be for javascript and does give correct answers to this question. I'm hoping the new ecmascript might have a newer solution to this question

4
  • Honestly I think you should treat undefined fields as non-existent, that alone would solve your problem. Commented Sep 6, 2017 at 10:24
  • @TomášZato In this example if s.a is undefined, then the field is non-existent Commented Sep 6, 2017 at 10:27
  • I don't see your problem then. Commented Sep 6, 2017 at 10:57
  • FWIW, JavaScript is an implementation of ECMAScript. This answer from the duplicate provides a solution in ES6. Commented Sep 6, 2017 at 21:05

4 Answers 4

1

Try this:

var t = $.extend({}, {
    d: s.a ? 1 : undefined
});
Sign up to request clarification or add additional context in comments.

9 Comments

Isn't this same as what I specified in the question ? Or am I missing somehting ?
You didn't use the extend method. If you use it it will work.
If that is right, Why does it disappear on extend.. ?
Maybe I misunderstood your question. I thought you want to have only properties with values in t variable.
You got it spot on!, But my question was more theoretical, Why does i need to move it to another object using extend, for the undefined to really become the 'undefined' ?
|
1

You didn't really tell what problem you're trying to solve, but generally there are several solutions:

Generate t from a

This is good if the properties are supposed to be equal in value. Because your property names differ, you'll need to map them:

const source = {
   a: 10,
   b: 12
}
const propertyNameMap = {
   a: "alpha",
   b: "beta"
}
const target = {};
for(let i in source) {
    if(source.hasOwnProperty(i) && propertyNameMap[i]) {
        target[propertyNameMap[i]] = source[i];
    }
}

Property getters

If properties need unique conversions, but are dependent, you can define a getter for the target object. Note that this has obvious performance implications and isn't really much better than just generating the values statically, unless they change in time:

// eg. if alpha maps to ASCII letter:
Object.defineProperty(target, "alpha", {
    get: function() {return String.fromCharCode(a.a);},
    enumerable: true
});

Comments

0
let s = {
    a : 1
}

let data = {};
if(!!s.a) {
    data.a = s.a;
}

console.log(data);

2 Comments

Updated my question :-)
Try to explain how your code snippet will resolve the question..
0

You can use Object.assign() and if there is s.a property it will add properties of new object otherwise it won't do anything.

var s = {a:1,b:2}
var t = {}

Object.assign(t, s.a ? {d: 1} : null)
console.log(t)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.