array.prototype.map

Pull only certain properties from each object in an array, and add to new array

眉间皱痕 提交于 2021-01-29 11:55:48
问题 Attempting to learn javascript. I am trying to create Table Row data from data in state (being sent to components via Context API): myArr = [ {id: 1, name: AA, desc: DescA, color: red}, {id: 2, name: BB, desc: DescB, color: orange}, {id: 3, name: CC, desc: DescC, color: green}, {id: 4, name: DD, desc: DescD, color: blue}, ] I would like to pull only name and color from each object, and create a new arr of objects. newArr = [ {name: AA, color: red} {name: BB, color: orange} {name: CC, color:

Can not add additional element to (mongoose) object

空扰寡人 提交于 2021-01-29 09:16:06
问题 I have a nodejs express application with an api to return data from a mongodb database. This is my mongoose model: const bookingSchema = new mongoose.Schema({ timestamp: { type: Date, default: Date.now, required: true }, tags: { type: [String], required: true }, amount: { type: Number, required: true }, type: { type: String, required: true, enum: ['expense', 'income'] } }) When I'm calling the api with the path /api/bookings/listbymonth/2019/1 this function inside the backend gets called:

Multiple queries in a .map function

孤者浪人 提交于 2021-01-07 01:36:16
问题 const returnPostData = async (req, res, initialPostsQueryArray) => { try{ const promises = initialPostsQueryArray.map( async (post) => { let voteCount = 0; let voted = false; let liked = false; let userHandle; let userImageUrl; let votingOptionsDictionary; let userVoteOption; voteCount = sumValues(post.voting_options); pool.query('SELECT userhandle, image_url FROM users WHERE id = $1 ', [post.user_id], (error, results) => { if (error) { console.log(error); return res.json({'Error': error

Multiple queries in a .map function

大憨熊 提交于 2021-01-07 01:33:15
问题 const returnPostData = async (req, res, initialPostsQueryArray) => { try{ const promises = initialPostsQueryArray.map( async (post) => { let voteCount = 0; let voted = false; let liked = false; let userHandle; let userImageUrl; let votingOptionsDictionary; let userVoteOption; voteCount = sumValues(post.voting_options); pool.query('SELECT userhandle, image_url FROM users WHERE id = $1 ', [post.user_id], (error, results) => { if (error) { console.log(error); return res.json({'Error': error

How to render nested array elements in React?

断了今生、忘了曾经 提交于 2020-07-05 22:49:38
问题 I want to render nested array elements. To render elements I used .map but it is not working for second array. Using list=[{value: 'One', list:[{value: 'abc', selected: false}, {value: 'efg', selected: false}]}, {value: 'Two', list: [{value: 'psr', selected: false}]}]; list.map((item, index) => { return ( <div key={index}> <ul >{item.value}</ul> item.list.map((subitem, i) => { return ( <ul >{subitem.value}</ul> ) }) </div> ) }) Am I missing anything here? Thanks 回答1: Try this. You missed { }

How to render nested array elements in React?

…衆ロ難τιáo~ 提交于 2020-07-05 22:46:08
问题 I want to render nested array elements. To render elements I used .map but it is not working for second array. Using list=[{value: 'One', list:[{value: 'abc', selected: false}, {value: 'efg', selected: false}]}, {value: 'Two', list: [{value: 'psr', selected: false}]}]; list.map((item, index) => { return ( <div key={index}> <ul >{item.value}</ul> item.list.map((subitem, i) => { return ( <ul >{subitem.value}</ul> ) }) </div> ) }) Am I missing anything here? Thanks 回答1: Try this. You missed { }

How do I replace a string with integers in a multi-dimensional array

别等时光非礼了梦想. 提交于 2020-01-05 08:35:11
问题 So I have an array of objects : var arr = [ {name: 'John', cars: '2', railcard: 'yes', preferences: ['taxi', 'tram', 'walking']}, {name: 'Mary', cars: '0', railcard: 'no', preferences: ['cyling', 'walking', 'taxi']}, {name: 'Elon', cars: '100000', railcard: 'no', preferences: ['Falcon 9', 'self-driving', 'Hyper-loop']} ]; I'm trying to transform the above array using map, filter, an reduce. I'm having trouble altering the original array even though I can easily isolate a specific data set.