Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将具有值的对象转换为具有值的数组_Javascript_Node.js_Reactjs - Fatal编程技术网

Javascript 如何将具有值的对象转换为具有值的数组

Javascript 如何将具有值的对象转换为具有值的数组,javascript,node.js,reactjs,Javascript,Node.js,Reactjs,我需要解析JSON并将其放入一个具有相同值的数组中,然后使用池将其传递到我的PostgreSQL数据库中。查询(消息,值)如果存在多个值,其中的值应该是一个数组,当前我的对象如下所示 { symbol: 'AAPL', beta: 1.1792937836342379, debtEquity: 1.19, dividendYield: 0.006304622435881964, peRatio: 37.02, employees: 142011, priceBook:

我需要解析JSON并将其放入一个具有相同值的数组中,然后使用
池将其传递到我的PostgreSQL数据库中。查询(消息,值)
如果存在多个值,其中的值应该是一个数组,当前我的对象如下所示

{
  symbol: 'AAPL',
  beta: 1.1792937836342379,
  debtEquity: 1.19,
  dividendYield: 0.006304622435881964,
  peRatio: 37.02,
  employees: 142011,
  priceBook: 24.66992282382262,
  psRatio: 7.9,
  putCallRatio: 0.845692355591619,
  revenueShare: 15.22,
  enterpriseValueRevenue: 8.17,
  nextEarnings: '2020-10-18',
  eps: 3.3888
}
但我需要它看起来像这样:

[
  symbol: 'AAPL',
  beta: 1.1792937836342379,
  debtEquity: 1.19,
  dividendYield: 0.006304622435881964,
  peRatio: 37.02,
  employees: 142011,
  priceBook: 24.66992282382262,
  psRatio: 7.9,
  putCallRatio: 0.845692355591619,
  revenueShare: 15.22,
  enterpriseValueRevenue: 8.17,
  nextEarnings: '2020-10-18',
  eps: 3.3888
]
var query = pool.query(
          `INSERT INTO advancedstats(symbol, beta, debtequity, dividendyield, peratio, employees,
          pricebook, psratio, putcallratio, revenueshare, enterprisevaluerevenue, nextearnings, 
          eps) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)`,
          newData
        );
[
  'AAPL',
  1.1792937836342379,
  1.19,
  0.006304622435881964,
  37.02,
  142011,
  24.66992282382262,
  7.9,
  0.845692355591619,
  15.22,
  8.17,
  '2020-10-18',
  3.3888
]
我尝试了
JSON.parse
JSON.stringify
,但它只是返回了一个语法错误,最好的处理方法是什么,我的池查询也如下所示:

[
  symbol: 'AAPL',
  beta: 1.1792937836342379,
  debtEquity: 1.19,
  dividendYield: 0.006304622435881964,
  peRatio: 37.02,
  employees: 142011,
  priceBook: 24.66992282382262,
  psRatio: 7.9,
  putCallRatio: 0.845692355591619,
  revenueShare: 15.22,
  enterpriseValueRevenue: 8.17,
  nextEarnings: '2020-10-18',
  eps: 3.3888
]
var query = pool.query(
          `INSERT INTO advancedstats(symbol, beta, debtequity, dividendyield, peratio, employees,
          pricebook, psratio, putcallratio, revenueshare, enterprisevaluerevenue, nextearnings, 
          eps) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)`,
          newData
        );
[
  'AAPL',
  1.1792937836342379,
  1.19,
  0.006304622435881964,
  37.02,
  142011,
  24.66992282382262,
  7.9,
  0.845692355591619,
  15.22,
  8.17,
  '2020-10-18',
  3.3888
]

其中,
newData
是当前对象

如果像这样查看数组输出(很可能是):

[
  symbol: 'AAPL',
  beta: 1.1792937836342379,
  debtEquity: 1.19,
  dividendYield: 0.006304622435881964,
  peRatio: 37.02,
  employees: 142011,
  priceBook: 24.66992282382262,
  psRatio: 7.9,
  putCallRatio: 0.845692355591619,
  revenueShare: 15.22,
  enterpriseValueRevenue: 8.17,
  nextEarnings: '2020-10-18',
  eps: 3.3888
]
var query = pool.query(
          `INSERT INTO advancedstats(symbol, beta, debtequity, dividendyield, peratio, employees,
          pricebook, psratio, putcallratio, revenueshare, enterprisevaluerevenue, nextearnings, 
          eps) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)`,
          newData
        );
[
  'AAPL',
  1.1792937836342379,
  1.19,
  0.006304622435881964,
  37.02,
  142011,
  24.66992282382262,
  7.9,
  0.845692355591619,
  15.22,
  8.17,
  '2020-10-18',
  3.3888
]
您可以使用
Object.values(variable)
将对象转换为这样的数组。然而,这个操作并不能保证变量的顺序——这对于您的案例很重要。为了强制执行顺序,我建议使用一个函数,该函数接受数据对象,并使用属性名称(按顺序)数组循环真实属性和构建结果

因此:

const propertyOrder = ['symbol', 'beta', 'debtEquity', ......];
const results = propertyOrder.map((key) => obj[key]);

您想要的输出无效。数组中只有成员,不能有
key:value
对。JS不支持您所需的输出查看您可能想要执行的文档:
pool.query(“此处查询…”,[obj.symbol,obj.beta,obj.debteequity,…)
其中
obj
是您当前的对象。要授予特定的值顺序,请创建一个属性名称数组,然后将它们映射到相应的值<代码>常量比例器=[“符号”,“beta”和…]然后
常量值=比例器.map(name=>obj[name])