Javascript - 映射函数中的哈希数组
Javascript - array of hash in a map function
我必须 return 映射函数中的哈希数组,如下所示:
results = [{title: "abc", category:"abcd", price: 23}, {title: "abc2", category:"abcd2", price: 24}]
我可以在 Ruby 中执行此操作,但我不知道如何在 Javascript 中执行此操作。我试过如下,但出现语法错误 ": unexpected"
const data = await page.content();
let results = await page.$$eval(
'.div',
divs => divs.map((div, index) => {
title: "abc",
category: "abcd",
price: 23
}
)
);
您的牙套周围需要括号 ()
{}
:
这是有效的:
let func = (div, index) => ({
title: "abc",
category: "abcd",
price: 23
})
这是无效的:
let func = (div, index) => {
title: "abc",
category: "abcd",
price: 23
}
之所以需要括号,是因为否则大括号将被解释为代码块而不是对象文字。
我必须 return 映射函数中的哈希数组,如下所示:
results = [{title: "abc", category:"abcd", price: 23}, {title: "abc2", category:"abcd2", price: 24}]
我可以在 Ruby 中执行此操作,但我不知道如何在 Javascript 中执行此操作。我试过如下,但出现语法错误 ": unexpected"
const data = await page.content();
let results = await page.$$eval(
'.div',
divs => divs.map((div, index) => {
title: "abc",
category: "abcd",
price: 23
}
)
);
您的牙套周围需要括号 ()
{}
:
这是有效的:
let func = (div, index) => ({
title: "abc",
category: "abcd",
price: 23
})
这是无效的:
let func = (div, index) => {
title: "abc",
category: "abcd",
price: 23
}
之所以需要括号,是因为否则大括号将被解释为代码块而不是对象文字。