1

My goal is to create an array of JavaScript objects and would like the output to be in a format like below:

journal = [
  {
    events: ["work", "ice cream", "cauliflower",
      "lasagna", "touched tree", "brushed teeth"
    ],
    squirrel: false
  },
  {
    events: ["weekend", "cycling", "break", "peanuts",
      "soda"
    ],
    squirrel: true
  }

];

The goal is to build from the following function with both given parameters (events and squirrel)

let journal = [];

function addEntry(events, squirrel) {
  ...........
  ...........
}

I wrote the code below and the output is giving me an error: "false is not a function". How can I fix that error and get the output expected? Thanks

let journal = [];

function addEntry(events, squirrel) {
  journal.push(
    ({
      events: ["work", "ice cream", "cauliflower", "lasagna", "touched tree", "brushed teeth"]
    }, false)
    ({
      events: ["weekend", "cycling", "break", "peanuts", "soda"]
    }, true)
  )
}
addEntry(console.log(journal));

2 Answers 2

2

When you push multiple values using push you need to separate them by ,. here you have (obj,false)() in this format, since the comma operator returns the last operand, so you actually end up doing false()

let journal = [];

function addEntry(events, squirrel) {
  journal.push(
    ({
      events: ["work", "ice cream", "cauliflower", "lasagna", "touched tree", "brushed teeth"]
    }, false),
    ({
      events: ["weekend", "cycling", "break", "peanuts", "soda"]
    }, true)
  )
}
addEntry(journal);
console.log(journal)

Here if you intend to push just object you don't need wrapping () and also if you need more than one property in your object you can add inside object, like this

 {
   key: value,
   key: value
 }

let journal = [];

function addEntry(events, squirrel) {
  journal.push({...events,squirrel})
}
addEntry({
    events: ["work", "ice cream", "cauliflower", "lasagna", "touched tree", "brushed teeth"]},true);
addEntry({
    events: ["weekend", "cycling", "break", "peanuts", "soda"]},false)
console.log(journal)

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

10 Comments

@Stack1 if you need multiple properties inside a object,, you need to add it as key value pair separated by ,, refresh and see the updated answer
The way you did is as the first object was the first parameter (events) and the second object was the second parameter (squirrel). I have to have true or false since in each object there should be the first and second parameter.
I guess what they're looking for is just addEntry(events, squirrel) = .push({events, squirrel})
@Stack1 i am not getting what you're saying. can you explain with an example ? can you update your question with the desired output ?
@georg yes may be, it so hard to guess this is what OP meant or not, with the provided example code
|
0

Your syntax in is incorrect

  1. You passed result of console.log method to addEntry method, this is no meaning

  2. You used () in your addEntry method ({ events: [...] }, false), this is a syntax error, you need to push an object (wrapper by {}, like this: ({ events: [...], squirrel : false })

This is example code:

let journal = [];

function addEntry(events) {
  events.forEach(event => journal.push(event));
}

let events = [
    {
        events: ["work", "ice cream", "cauliflower",
          "lasagna", "touched tree", "brushed teeth"
        ],
        squirrel: false,
    },
    {
        events: ["weekend", "cycling", "break", "peanuts",
          "soda"
        ],
        squirrel: true
    }
]

addEntry(events);
console.log(journal);

1 Comment

You changed the code by removing the parameter squirrel. It's easy like that. They want to keep both parameters and get the expected output.

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.