Wednesday 20 March 2019

How to convert javascript objects into an array of javascript objects with node.js

Hello Reddit community,​First time poster, long time reader.​I am very new to node.js so please be gentle :)I am trying to write a fun server-less app within AWS in order to learn node.js.​I have no issues writing data to my DynamoDB table.​The issue I am facing is that I am using the following method to scan the database to get a list of email addresses:​const AWS = require('aws-sdk'); const docClient = new AWS.DynamoDB.DocumentClient(); const { TableName } = process.env; exports.handler = (event) => { var params = { TableName: TableName, ProjectionExpression: "#email, USERID", ExpressionAttributeNames: { "#email": "email" } }; docClient.scan(params, onScan); function onScan(err, data) { if (err) { console.error(JSON.stringify(err, null, 2)); } else { data.Items.forEach(function (itemdata) { const itemdataEailArray = []; itemdataEailArray.push(itemdata.email); console.log(itemdataEailArray); }); if (typeof data.LastEvaluatedKey != "undefined") { console.log("Scanning for more..."); params.ExclusiveStartKey = data.LastEvaluatedKey; docClient.scan(params, onScan); } } } } ​This returns the following:[ 'lo2l@lol.com' ] [ 'woooooooooooo2@wooooooooooooooooo456789oo2.com' ] [ 'hello@hello.com' ] [ 'lo2l@lol.com' ] [ 'hello@hello.com' ] [ 'lol@lol.com' ] ​​How can I convert that out put to a javascript array that looks like:[ 'lo2l@lol.com', 'woooooooooooo2@wooooooooooooooooo456789oo2.com', 'hello@hello.com', 'lo2l@lol.com', 'hello@hello.com', 'lol@lol.com' ] ​Its the last thing I need to do for my back end functions to get it all working.Any help is much appreciated in advance.​​

Submitted March 20, 2019 at 12:06PM by zadowsmash

No comments:

Post a Comment