简体   繁体   English

为什么在创建对象数组时出现错误?

[英]Why am I getting an error when creating an array of objects?

My goal is to create an array of JavaScript objects and would like the output to be in a format like below:我的目标是创建一个 JavaScript 对象数组,并希望 output 采用如下格式:

 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)目标是使用给定参数(事件和松鼠)从以下 function 构建

 let journal = []; function addEntry(events, squirrel) {........... ........... }

I wrote the code below and the output is giving me an error: "false is not a function".我写了下面的代码,output 给了我一个错误:“false 不是函数”。 How can I fix that error and get the output expected?如何修复该错误并获得预期的 output? 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));

When you push multiple values using push you need to separate them by , .当您使用push推送多个值时,您需要用,分隔它们。 here you have (obj,false)() in this format, since the comma operator returns the last operand, so you actually end up doing false()这里你有这种格式的(obj,false)() ,因为逗号运算符返回最后一个操作数,所以你实际上最终做了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在这里,如果您打算只推送 object,则不需要包装() ,并且如果您在 object 中需要多个属性,则可以在 object 中添加多个属性,如下所示

 {
   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)

Your syntax in is incorrect您的语法不正确

  1. You passed result of console.log method to addEntry method, this is no meaning您将console.log方法的结果传递给addEntry方法,这是没有意义的

  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 })您在 addEntry 方法中使用了() ({ events: [...] }, false) ,这是一个语法错误,您需要推送一个 object (由{}包装,如下所示: ({ 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在数组中遍历JSON对象时,为什么会出现语法错误? 如何正确提取JSON? - Why am I getting a Syntax Error when looping through JSON Objects in an array? How do I extract JSON correctly? 在带有数组的对象上使用它时,为什么会出现$ parse错误? - Why I am getting $parse error when use it on object with array? "为什么这个多维数组出现错误?" - Why am I getting an error with this multidimensional array? 为什么我会收到“对象作为 React 子项无效”错误? - Why am I getting "Objects are not valid as a React child" error? 在 class 中创建函数时出现错误 - I am getting an error when creating functions in a class 创建新的 React 应用程序时出现错误 - I am getting an error when creating a new react app 为什么当我生成错误时我收到字符串错误? - Why when i generate an Error i am getting an String Error? 将数组传递给模态时,为什么会出现角度未知的提供者注入器错误? - Why am I getting an angular unknown provider injector error when passing an array to a modal? 为什么我在使用数组 concat 时不断收到 TypeScript 错误“没有重载匹配此调用”? - Why am I keep getting TypeScript error “No overload matches this call” when using Array concat? 为什么我在此二维数组上出现错误? - Why am I getting an error with this 2d array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM