11

I have static array constant of objects something similar to below.

export const EMPLOYEES = [
  {
    id: 2,
    name: ‘John’,
  },
  {
    id: 3,
    name: ‘Doe’,
  },
  {
    id: 4,
    name: ‘Bull’,
  },
  {
    id: 5,
    name: ‘Scott’,
  },
];

Now I need to add the last element only based on if some condition is true. Some this like if isAmerican() is true.

Can somebody help me here how to add element based on the condition? Thanks.

4
  • 2
    Can't modify constants like that. Commented Oct 11, 2018 at 6:26
  • 1
    You can't modify what the constant points to, but you can push to the array. So do something like: if (isAmerican()) { employees.push({ id: 6, name: 'Timmy' }) } Commented Oct 11, 2018 at 6:31
  • Now I need to have the last element only based on if some condition is true. What do you mean by have? Remove? Get? Commented Oct 11, 2018 at 6:38
  • @FrankerZ Sure you can, nothing in const makes the array immutable. Commented Oct 11, 2018 at 6:54

3 Answers 3

30

You can do it using spread operator:

export const EMPLOYEES = [
    {
        id: 2,
        name: "John",
    },
    {
        id: 3,
        name: "Doe",
    },
    {
        id: 4,
        name: "Bull",
    },
    {
        id: 5,
        name: "Scott",
    },
    ... isAmerican() ? [{ id: 6, name: "Jemmy"}] : []
];
Sign up to request clarification or add additional context in comments.

Comments

2

You should never modify (or try to modify) a constant. I can see two ways you can do this:

  1. Create a pure function to return a new constant with the new object added to the array
  2. Use a spread operator in the definition of the constant

Option 1: Pure function

function makeNewArray(array, objectToAppend, isAmerican) {
    return isAmerican ? [...array, objectToAppend] : array
}

const EMPLOYEES = [
    {
        id: 2,
        name: "John",
    },
    {
        id: 3,
        name: "Doe",
    },
    {
        id: 4,
        name: "Bull",
    },
    {
        id: 5,
        name: "Scott",
    }
];

const arrayWithAmerican = makeNewArray(EMPLOYEES, { id: 6, name: "American Frank"}, true);
const arrayWithoutAmerican = makeNewArray(EMPLOYEES, { id: 6, name: "Not American Frank"}, false);

console.log(arrayWithAmerican);
console.log(arrayWithoutAmerican);

Option 2: Spread operator

function isAmerican(){
    // generic code here.
    return true;
}

const EMPLOYEES = [
    {
        id: 2,
        name: "John",
    },
    {
        id: 3,
        name: "Doe",
    },
    {
        id: 4,
        name: "Bull",
    },
    {
        id: 5,
        name: "Scott",
    },
    ... isAmerican() ? [{ id: 6, name: "American Frank"}] : []
];

Comments

1

If the condition will be fulfilled, simply push an object to your EMPLOYEES array:

let isAmerican = true;

const EMPLOYEES = [
  {
    id: 2,
    name: "John",
  },
  {
    id: 3,
    name: "Doe",
  },
  {
    id: 4,
    name: "Bull",
  },
  {
    id: 5,
    name: "Scott",
  },
];

if(isAmerican) {
    EMPLOYEES.push({
    id: 6,
    name: "Frank"
  })
}
console.log(EMPLOYEES)

Fiddle: https://jsfiddle.net/rqx35pLz/

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.