All Questions
5,703
questions
443
votes
12
answers
367k
views
JavaScript, Node.js: is Array.forEach asynchronous?
I have a question regarding the native Array.forEach implementation of JavaScript: Does it behave asynchronously?
For example, if I call:
[many many elements].forEach(function () {lots of work to do})...
337
votes
4
answers
336k
views
How to search in array of object in mongodb
Suppose the mongodb document(table) 'users' is
{
_id: 1,
name: {
first: 'John',
last: 'Backus'
},
birth: new Date('Dec 03, 1924'),
death: new Date('Mar 17, 2007'),
...
148
votes
5
answers
84k
views
Dumping whole array: console.log and console.dir output "... NUM more items]"
I am trying to log a long array so I can copy it quickly in my terminal. However, if I try and log the array it looks like:
['item',
'item',
>>more items<<<
... 399 more items ]
...
129
votes
8
answers
130k
views
MongoDB, remove object from array
Doc:
{
_id: 5150a1199fac0e6910000002,
name: 'some name',
items: [{
id: 23,
name: 'item name 23'
},{
id: 24,
name: 'item name 24'
}]
}
Is there a way to pull a ...
126
votes
4
answers
191k
views
Looping through files in a folder Node.JS
I am trying to loop through and pick up files in a directory, but I have some trouble implementing it. How to pull in multiple files and then move them to another folder?
var dirname = 'C:/...
121
votes
6
answers
148k
views
MongoDB select where in array of _id?
is possible in mongo db to select collection's documents like in SQL :
SELECT * FROM collection WHERE _id IN (1,2,3,4);
or if i have a _id array i must select one by one and then recompose the array/...
96
votes
1
answer
56k
views
Why does gulp.src not like being passed an array of complete paths to files?
I'm attempting to pass gulp.src an array of files that I want it to deal with. This is the array as it stands.
['bower_components/jquery/jquery.js',
'bower_components/superscrollorama/js/greensock/...
94
votes
7
answers
114k
views
Uncaught TypeError: Object.values is not a function JavaScript
I have a simple object like the one below:
var countries = {
"Argentina":1,
"Canada":2,
"Egypt":1,
};
I need to create two arrays. The first array is an array of all the keys from the ...
93
votes
5
answers
99k
views
JavaScript: detect if argument is array instead of object (Node.JS)
How should I detect if the argument is an array because typeof [] returns 'object' and I want to distinguish between arrays and objects.
It is possible that object will look like {"0":"string","1":"...
73
votes
5
answers
64k
views
Equivalent to LINQ's Enumerable.First(predicate)
In C#, we have Enumerable.First(predicate). Given this JavaScript code:
function process() {
var firstMatch = ['a', 'b', 'c'].filter(function(e) {
return applyConditions(e);
}).shift();
if(...
50
votes
7
answers
96k
views
How to replace elements in array with elements of another array
I want to replace elements in some array from 0 element, with elements of another array with variable length. Like:
var arr = new Array(10), anotherArr = [1, 2, 3], result;
result = anotherArr....
49
votes
11
answers
176k
views
Filter and delete filtered elements in an array
I want to remove specific elements in the original array (which is var a). I filter() that array and splice() returned new array. but that doesn't affect the original array in this code. How can I ...
49
votes
10
answers
38k
views
Check if every element in one array is in a second array
I have two arrays and I want to check if every element in arr2 is in arr1. If the value of an element is repeated in arr2, it needs to be in arr1 an equal number of times. What's the best way of doing ...
48
votes
7
answers
73k
views
javascript array as a list of strings (preserving quotes)
I've got an array of strings. When I use .toString() to output it the quotes are not preserved. This makes it hard to build the mysql query using an "in". Consider the following:
SELECT * FROM ...
43
votes
4
answers
23k
views
Mongoose save() not updating value in an array in database document
I am trying to update a document in a collection (units) using GUI and after it gets updated I want to update the value (user.Units which is an array of Unit names) in collection (users). If the ...
39
votes
4
answers
24k
views
In node.js, why is there a util.isArray and an Array.isArray?
I just noticed the API docs for Node.js v0.10.26 provide for util.isArray,
util.isArray(object)# Returns true if the given "object" is an Array.
false otherwise.
var util = require('util');
...
38
votes
2
answers
98k
views
NodeJS: How to remove duplicates from Array [duplicate]
I have an array:
[
1029,
1008,
1040,
1019,
1030,
1009,
1041,
1020,
1031,
1010,
1042,
1021,
1030,
1008,
1045,
1019,
1032,
1009,
...
38
votes
8
answers
32k
views
How to log JavaScript objects and arrays in winston as console.log does?
I was looking at top Node logging systems: npmlog, log4js, bunyan and winston and decided to use winston for having the most npm monthly downloads.
What I want to set up is custom logger which I ...
35
votes
4
answers
135k
views
NodeJS find object in array by value of a key [duplicate]
I am trying to get an object in an array by the value of one of its keys.
The array:
var arr = [
{
city: 'Amsterdam',
title: 'This is Amsterdam!'
},
{
...
32
votes
4
answers
18k
views
array.prototype.flat is undefined in nodejs
Given is the following js command:
[].flat;
Executing it in a browser (chrome/firefox): returns function
Executing it with nodejs v10.13.0: returns undefined
Now I am wondering what other methods ...
30
votes
5
answers
35k
views
Does spread operator affect performance?
I am considering the below two approaches for building an array of objects:
Approach 1 (list all properties, even if duplicated among objects):
const employees = [
{
company: 'ABC',
...
29
votes
5
answers
111k
views
A list of tuples in Javascript [closed]
Let's say I receive a JSON-string containing information about people.
Now I loop through all the JSON-objects and extract the name and age or each person.
How would I store these as tuples in a list ...
26
votes
10
answers
52k
views
Copying an array of objects into another array in javascript (Deep Copy)
Copying an array of objects into another array in javascript using slice(0) and concat() doesnt work.
I have tried the following to test if i get the expected behaviour of deep copy using this. But ...
26
votes
12
answers
47k
views
Combine json arrays by key, javascript
I need to combine two json arrays, delivered by two rest services. The entries with the same "id" belong together.
json1 = [{id:1,name:'aaa'},
{id:5,name:'ccc'},
{id:3,name:'bbb'}
];
...
24
votes
5
answers
21k
views
Comparing arrays in chai
I'm writing some tests with chai and chai-as-promised (and more frameworks, but it doesn't matter in this case) and I need to check if array I get from a web-page is same as a predefined array. I ...
23
votes
3
answers
54k
views
Mongoose, check if value exists in an array of objects
I have a collection with: I want to use the $in operator
Person = {
name: String,
members: [ {id: String, email: String}... {}]
}
I use this so far:
Person.find({members: {"$in": [id1]}})
But I ...
21
votes
3
answers
45k
views
How to handle Array buffer allocation failed in nodejs?
I have successfully upload image in nodejs and I am using multer for that.
But sometime " Array buffer allocation failed " error occurs and I want to handle that error. I tried to keep the code in try-...
20
votes
5
answers
40k
views
Check if an array contains something other than null in javascript?
I have an array that will most likely always look like:
[null, null, null, null, null]
sometimes this array might change to something like:
["helloworld", null, null, null, null]
I know I could ...
20
votes
5
answers
33k
views
How to remove Object from array using mongoose
I'm trying to remove an object from an array in a document using mongoose.
The Schema is the following:
var diveSchema = new Schema({
//irrelevant fields
divers: [{
user: { type: Schema....
19
votes
3
answers
52k
views
Export array to be used in another javascript file
In my post request, I want to check if thisString exists in another javascript file array.
Array.js
exports.names = [
'John',
'Mary'
]
Main.js
if (names.includes(thisString)) {
...do ...
19
votes
3
answers
40k
views
Javascript - flatMap method over array - (flatMap is not a function)
According to the Mozilla Developer Website:
The flatMap() method first maps each element using a mapping function, then flattens the result into a new array. It is identical to a map followed by a ...
19
votes
2
answers
65k
views
nodejs conversion from buffer data to byte array
I want to convert buffer data to byte array. Here's what I've tried
import * as fs from 'fs';
[...]
event:(data) => {
fs.readFile(data, function(err, data) {
var arrByte= new Uint8Array(...
17
votes
1
answer
34k
views
SyntaxError: Unexpected reserved word "await", node.js is correct version
I am trying to run a function asynchronously 20 times.
I have the function definition:
import axios from 'axios';
async function updateUser (url, user) {
return new Promise((resolve, reject) =...
16
votes
5
answers
31k
views
How to check if array contains at least one object ?
I want to check if array contains object or not. I am not trying to compare values just want to check in my array if object is present or not?
Ex.
$arr = ['a','b','c'] // normal
$arr = [{ id: 1}, {...
16
votes
4
answers
13k
views
Iterate through an array in blocks of 50 items at a time in node.js
I'm new to node.js and am currently trying to code array iterations. I have an array of 1,000 items - which I'd like to iterate through in blocks of 50 items at a time due to problems with server load....
16
votes
9
answers
10k
views
Duplicate an array an arbitrary number of times (javascript)
Let's say I'm given an array. The length of this array is 3, and has 3 elements:
var array = ['1','2','3'];
Eventually I will need to check if this array is equal to an array with the same elements,...
16
votes
6
answers
5k
views
Group similar strings from an array in Node.js
Let's say I have a collection of different URLs in an array:
var source = ['www.xyz.com/Product/1', 'www.xyz.com/Product/3', 'www.xyz.com/Category/1', 'somestring']
What would be a good way to ...
15
votes
3
answers
153k
views
Creating a JSON Array in node js
I need to create in my server written in node js a JSON string to be sent to the client when this request it. The problem is that this JSON depends on the available data in the server, thus, the size ...
15
votes
1
answer
51k
views
Typescript/nodejs: variable implicitly has type 'any' in some locations
I'm using typescript with nodejs to init DB data, I want to declare a global array variable to use inside functions :
export { };
import {Address,CodePostal} from 'api/models';
const faker = require(...
14
votes
1
answer
16k
views
mongoose update with push operations on array and set operation on object
I have this mongoose schema
var ContactSchema = module.exports = new mongoose.Schema({
name: {
type: String,
required: true
},
phone: {
type: Number,
required: true,
},
...
14
votes
2
answers
23k
views
JSON.parse() on a large array of objects is using way more memory than it should
I generate a ~200'000-element array of objects (using object literal notation inside map rather than new Constructor()), and I'm saving a JSON.stringify'd version of it to disk, where it takes up 31 ...
14
votes
2
answers
68k
views
Cannot read property 'Symbol(Symbol.iterator)' of undefined
I'm trying to loop through an array to check if it contains any item that passes a specified function. I do this by adding a .any() prototype to the Array object:
Array.prototype.any = (comparator) =&...
14
votes
3
answers
2k
views
Building array of objects from parsed csv files in node
I have multiple csv files of the form
model1A
model1B
model2A
model2B
where each csv is an array i.e. model1A = [1, 1, 1]
I want to parse these csvs and create a single array containing all these ...
13
votes
2
answers
23k
views
nodeJs huge array processing throws RangeError: Maximum call stack size exceeded
This is part of code to proceed big number of entries ( originally its works with file system and make some operations with files) . Is there any nice way to bypass the limitation and prevent ...
13
votes
2
answers
47k
views
Mongoose find array with $in
Team.find({
'_id': { $in: [
teamIds
] }
}, function(err, teamData) {
console.log("teams name " + teamData);
});
This code gives us undefined back.. But in ...
13
votes
4
answers
29k
views
Updating nested array inside array mongodb [duplicate]
I have the following mongodb document structure:
[
{
"_id": "04",
"name": "test service 4",
"id": "04",
"version": "0.0.1",
"title": "testing",
"...
13
votes
1
answer
35k
views
Finding a match in an array field
In my image sharing application you can create albums and add images to them. When an image is deleted from the site, it should as well be removed from the album(s) that stores references to the image ...
13
votes
4
answers
12k
views
Javascript ES6, best way to remove item from array if it exists or add if doesn't exists
I'm writing some code here to add an element on an array with the following logic
If the element exists on the array, the function should remove the element from the array and return the array ...
12
votes
1
answer
19k
views
How do I display data through components with Vue.js (using Vueify)?
I'm having trouble getting data to display in my Vue components. I'm using Vueify and I'm trying to load an array of listings from the listings.vue component and I keep getting errors. Also, I don't ...
12
votes
3
answers
16k
views
Add milliseconds delay to Array.map calls which returns Array of promises
My need is simple. I would like to delay calls to sendEmail by 100 milliseconds. The email service provider permits at most sending 10 emails per second.
Note, however, though .map is synchronous, ...