0

I think this is a relatively simple thing I'm trying to do, but unsure how to accomplish it. Essentially I have an array to which I want to push a value (parent) ONLY if that value exists. I'm trying to accomplish it like so, but this doesn't compile. I want to only set the parent index if there is a value to it, I don't want parent: null

I'm trying to get a cleaner solution and not two pushes dependant upon the value being set. I would prefer the one push to encapsulate the check if possible

array.push({
    id: id,
    name: name,
    parent ? parent: parent : null
})

5 Answers 5

1

You could use && operator, shorthand property names and spread syntax like this

array.push({
    id,
    name,
    ...(parent && { parent })
})

If parent is null, the && operator will return null and spreading null returns an empty object. If parent has a value, { parent } object created will be spread inside the object.

Note: This will also skip the parent key for all falsy parent values like 0 and "". But, you are using ternary operator on parent. So, I'm assuming those values aren't valid.

Here's a snippet:

function create(parent) {
  console.log({
    id: 1,
    ...(parent && { parent })
  })
}

[null, undefined, 5, "a string"].map(create)

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

4 Comments

Just a question on this. If parent is the result of a function, how do I set parent to that function result. If I follow the above syntax, I get parent is not defined.
I didn't understand your question. Can you create a fiddle? Is parent variable created and available in that scope?
Not the best fiddle but along this gist: jsfiddle.net/jxwba82p Assume value of parent will change and sometimes func will return null
@Nouman you can destructure the array returned by the function to 2 variables const [parentId, parentName] = getParent(parent) ?? [] And add the keys conditionally jsfiddle.net/adigas/ymgqnhxv/1
1

I think you can use the function Object.assign as follow

array.push(Object.assign({
    id: id,
    name: name,
}, parent ? {parent} : {}))

Be careful with the falsy values.

Comments

0

You can use ellipsis notation.

array.push({
    id: id,
    name: name,
    ...(parent ? {parent: parent} : {})
});

5 Comments

Why not just {parent}? and I think you did mean spread operator.
And what is if parent is set to 0?
The intent in the original code seems to be to omit it. But I'm going to wager that in the intended use case, if the value exists it's always an object, not a type with any falsey value.
Or none of the valid values are falsey -- e.g. 0 might not be a valid parent ID.
With the code you are right. But OP says: want to push a value (parent) ONLY if that value exists. and if set to 0 it's for me setted.
0

You can try something like this

let obj = {
  id: "id",
  name: "name",
  parent: null
}


let obj1 = {
  id: "id",
  name: "name",
  parent: "parent"
}



const array = [

  ...obj.parent ? [obj] : [],
];

const array2 = [

  ...obj1.parent ? [obj1] : [],
];


console.log(array)
console.log(array2)

Comments

0
let obj = {
    id: id,
    name: name
};

if (parent !== undefined)
    obj.parent = parent;

array.push(obj);

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.