500 questions
1
vote
1
answer
67
views
Angular FormBuilder with spread operator on object with arrays has FormControls of the wrong type
I'm using the Angular FormBuilder (import { FormBuilder } from '@angular/forms';) to create my FormGroup and inner FormControls as this right now:
formGroup = new FormBuilder().nonNullable.group({
....
0
votes
1
answer
48
views
Is the spread operator (...) available in AssemblyScript?
I couldn't find any information about this in the AssemblyScript docs.
Is the spread operator available in AssemblyScript? I would like to define an object like so:
{ ...params, category: "...
0
votes
2
answers
62
views
TypeScript misunderstanding. How to fix problem?
Have a code (typescriptPlayground)
problem is in spread operator.
in case of secondObjectFull i have an error. It is ok. firstObject cannot spread to secondFullMapped
But in case of thirdObjectFull ...
-2
votes
3
answers
63
views
JavaScript 2D arrays and returning matching rows
I have 2 arrays, each is like a database table row set.
array1 = [
['AB2C', 'Red', 113],
['BE4F', 'Green', 164],
['AE3G', 'Blue', 143],
]
array2 = [
[143, 'FabricB2', 'W5'],
[189, 'FabricC9', 'W4'],
[...
0
votes
0
answers
70
views
Type error when using generics and looping over functions
I have an object of methods which I am seeking to extend with copies of those methods but scoped to a given method e.g. the method fetchOnce() will have copies postOnce(), getOnce() etc.
The original ...
0
votes
3
answers
267
views
Prepend an element to each list in a list
Let's say we have the following base list:
[["foo"],["bar"]]
and the input string "a"
I want to prepend the input string to each list in the list, which results in:
[[&...
0
votes
1
answer
80
views
Spread not works for Array(16) _wgpuMatrix.mat4.invert return NUll filled array
For matrix transformation i use wgpu-matrix library.
I already have implemented raycast (hit object trigger) in other project. I wanna implement it also in my new project matrix-engine-wgpu.
In this ...
0
votes
2
answers
180
views
Is there an easy way to merge object arrays using javascript ellipsis or similar?
let arr = [
{id: 1, data: {foo: "bar 1"}},
{id: 2, data: {foo: "bar 2"}}
];
//In case the ID does not exist just add as a new element on the array
arr = [...arr, {id: 3, data: {foo: "bar 3"}}]
...
1
vote
1
answer
82
views
is there a typescript generic that handles T | T[] | "*"
I'm new to generics in typescript, it's confusing
is there a way to make the spread operator ... work in the snippet below
the issue
the line [ key: U, ...rest: Reg[U] ], doesn't work as I expect
the ...
2
votes
1
answer
356
views
How to create a type that enforces exclusion of properties in typescript when using spread operator?
I have a backend model with some properties that I want to be able to send to the frontend with certain properties excluded. I'd like to use typing to help me ensure they have been excluded.
I tried ...
1
vote
0
answers
32
views
extract partial attributes of an object using typescript types [duplicate]
how to extract partial attributes of an object using typescript types? is it even possible?
export interface Product {
brandName: string;
modelNo: string;
rate: number | null;
someOtherProp1: ...
1
vote
0
answers
43
views
Why does Javascript spread maintain defined keys that are set to undefined, and how do they differ from non-existent keys? [duplicate]
I have a code sample below that demonstrates this. I ran into this unexpected behavior of the spread operator where a key that gets set as undefined is not treated the same way keys that have never ...
1
vote
4
answers
177
views
Cannot use spread inside conditional operator [duplicate]
Suppose the following:
const arr = [1, 2, 3];
const insertValues = [-1, 0];
const condition = true;
arr.splice(0, 0, condition ? ...insertValues : insertValues);
This throws a syntax error:
...
0
votes
0
answers
60
views
Using immutability-helper or spread op to make changes to an object, what is better: repeatedly $set, or $merge only once an object with all changes?
In my state I have a dictionary of a flat object:
export interface INodeVisibility {
id: string;
level: number;
isExpanded: boolean;
}
export type NodeVisibilityDict = { [key: string]: ...
1
vote
1
answer
128
views
React State modification despite the fact I used spread operator to avoid direct modification of it
I tried to use spread operator to avoid modifications of my state without the use the set State. But despite that, my state is still modified.
I can eventually make a deepCopy of my state but I don't ...
0
votes
2
answers
84
views
Exploring React Shallow Copy Methods: Spread Operator vs Direct Assignment
I am currently working on a React project and encountered an interesting issue related to shallow copying objects. In my component, I have a form where I collect data, and I noticed that different ...
-1
votes
1
answer
133
views
Clarifying “error-prone” nuance of spread syntax vs. assignment in Javascript
I know that the assignment of one variable to another is done by shared reference so that when you set var c = b; and then you do some change change to c such as c.cool = 'new'; then b gets affected ...
3
votes
3
answers
323
views
Spread operator only returns the first value in Google apps script
I'm trying to insert some values in a Google sheet using a Google Apps Script, but when I'm spreading the range that I defined earlier, it only returns the first value.
I expected Logger.log(......
0
votes
0
answers
83
views
how to make spread syntax work with "export const pipes = _pipes" in NgModule's declarations
StackBlitz for this problem
I want to create some constant pipes and import them all together in app.module.ts.
It works when I use syntax like export const pipes = [MinValue, MaxValue], but does not ...
-2
votes
1
answer
182
views
How to copy object properties to another object by reference
I want to be able to bundle the properties of multiple objects into another object by reference, such that when I change the properties in the bundle object it mutates the original objects. I'm using ...
1
vote
2
answers
1k
views
How does JavaScript know the difference between the spread and rest operator?
The syntax is identical so how does JavaScript distinguish the two under the hood?
Does it look at the data type of the variable that is being operated on? Or where that variable is being used? Both ...
4
votes
8
answers
712
views
How to return key value pairs from map function using spread operator
I have an object and an array. Say:
const first = {
'key1': 'some date',
'key2': 'some date'
}
const second = ['key3', 'key4']
Then using spread syntax I want to combine then into single object....
2
votes
1
answer
61
views
Difference between assignment and unpacking intto one array to another
I have this code in JavaScript what is the difference between the lines A and B ?
const arr1 = [1,2,3,4,5]
const arr2 = [...arr1]; // Line A
const arr2 = arr1; // Line B
I want to know is it ...
1
vote
1
answer
24
views
typescript error when spreading `Parameters<F>`
This is the narrowed down code from my actual use case.
function wrapMe<F extends (...args: any) => any>(
f: F,
): (...args: Parameters<F>) => ReturnType<F> {
return ...
0
votes
1
answer
50
views
No Error in Typescript if spreading unknon property into variable
I have a playground for this question here.
In the following code, typescript does error on the assignment to t2_2 ("not assignable") but not on t2_1, where I have the "x" property ...
0
votes
0
answers
30
views
What is the use of spread syntax in function while creating a new array? [duplicate]
I understand about JavaScript spread syntax in general. But in the following code, I cannot understand why it is used when creating a new array of specific length:
const createArray = length => [......
1
vote
1
answer
317
views
Spread operator and destructuring
interface Item {
id: string;
title: string,
}
interface ItemState {
entities: { [id: string]: Item };
}
const toBeDeleted = { id: '2', title: 'item_2' };
const state: ItemState = {
...
8
votes
6
answers
3k
views
Destructuring an array using spread operator on left side of assignment to collect all remaining elements in a single variable
I want to perform Destructuring in php just like in javascript code below:
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(a,b,rest);
Output:
10 20 [ 30, 40, 50 ]
How can I preform that ...
1
vote
1
answer
416
views
Why does using spread synax(...) not honor type-safety for an expected return type in typescript
I've got a function which is expected to return an object with specific keys. In my case, I only want to add the keys conditionally, so I am using spread syntax to help with that.
However, when I use ...
0
votes
2
answers
136
views
JS Spread Operator to normal form?
I have the following code that includes Spread Syntax in JS
const opposite = function (f) { return (function (...args) { return !f(...args) }) };
can it be converted without using Spread Syntax? It ...
2
votes
2
answers
662
views
Mapping array inside another array using spread operator
I'm testing spread operator inside array to map another array values. Unfortunately, I have come up with weird behavior or I did it wrong. When I return 2 objects using map inside the array, it's only ...
2
votes
5
answers
1k
views
Javascript spread on array index to 'push' new item to nested array
Consider the following data:
let data = [
{ foo: true, bar: [ 1, 2, 3 ] },
{ foo: true, bar: [ 8, 9, ] }
];
I'm trying to push something to the nested bar array on index 1 using the spread ...
1
vote
1
answer
89
views
I'm trying to add to an array of objects that is broken into two inputs in React
So I have an array of objects where the keys are 'cost' and 'service' called estimate. You can add to the array by clicking 'Add' which adds a new index (i) to the array. The issue is on the first ...
1
vote
3
answers
107
views
JavaScript function for multi-level spread syntax
I'm working with a JavaScript object that gives the default values for a library I am working with
const defaults = {
alpha: alpha_val,
beta: {
a: {
i: beta_a_i_val,
ii: ...
1
vote
2
answers
63
views
How to change a array of object inside a object?
I have an object that contain a property with an array of objects which I have to change one object.
{id: '3688aa8f-e725-45f4-9513-363a1c019b34',
category: 'Entertainment: Film',
difficulty: 'easy',
...
1
vote
0
answers
2k
views
Spreading props in react as attributes?
I've read up on the es6 object spread operator and I've come across it being used in a react tutorial to spread props into a component. I can't understand how this is working though, for instance
...
1
vote
2
answers
293
views
Javascript: is there "Symbol.iterator" analogue for the object spread syntax - { ...obj }?
There is a well known symbol: Symbol.iterator, that, when defined as a generator function property on an object, allows the object to be used in a [...object] syntax. So, you can do, eg., this:
const ...
1
vote
2
answers
94
views
How to restrict function parameters types base on if the index of parameter is even or odd in typescript?
What I'm looking for is to have a function with infinite number of parameters and each parameter type is based on if its index is even or odd.
A contrived example:
flow(isMachineReady(), 'and', ...
0
votes
3
answers
197
views
what is the purpose of using spread operator here?
const defaultFormFields = {
displayName: "",
email: "",
password: "",
confirmPassword: "",
};
const SignUpForm = () => {
const [formFields, ...
1
vote
2
answers
1k
views
React, update state with .map() and spread operator in nested array of objects
I'm still a newbie in React and I've been struggling for some time with the topic mentioned above. I've got a state which looks like this:
const [questions, setQuestions] = React.useState([])
...
1
vote
2
answers
214
views
Why does `new Array(...[ 7 ])` produce `Array(7) [ <7 empty slots> ]`?
Why does this code print Array(7) [ <7 empty slots> ] (or an array of undefined, 7 times) instead of an array with the single element 7?
const a = [ 7 ];
console.log(new Array(...a));
0
votes
1
answer
45
views
Can you use a shallow copied variable in splice?
Apologies for the terrible title I didn't know how else to word it.
this is a kata im trying to complete in codeWars. Drone Fly-by.
basically the lamps parameter will be a string of 'x' characters and ...
0
votes
4
answers
103
views
JavaScript object shorthands
I have this clunky code here and hope there is any way to shorten this.
By the way, "req.query" is my query object from an nodejs express server.
const q = req.query
User.insertOne({...
4
votes
2
answers
6k
views
does C++ have spread operator?
can you do this in C++:
vector<int> v1={1,2}, v2={3,4};
vector<int> v3={...v1, ...v2, 5};
//v3 = {1,2,3,4,5}
What is the simplest way to do this with C++ ?
3
votes
1
answer
3k
views
A spread argument must either have a tuple type or be passed to a rest parameter [duplicate]
I am new to TypeScript and I am going to change my project to TypeScript.
But I got some error.
I have searched about the spread arguments but I can't figure it out correctly.
This is I tried so far.
...
1
vote
1
answer
142
views
spread object and add flag node.js
Here is my code:
let collection = database.collection('documents')
let savedDocumentsCollection = database.collection('savedDocuments')
let doc = await collection.find().toArray()
let saved_documents =...
0
votes
1
answer
160
views
TypeError: Invalid attempt to spread non-iterable instance *On an Object*
I have the following code as part of grabbing data from a query and appending it to a local state that is stored as an object. However, this following code using the spread operator (...) is not ...
4
votes
1
answer
301
views
Need explanation for array destructuring with spread operator
How does the spread operator within the array destructuring of y3 work?
The result would contain both 'lizard' and 'spock', but because of the spread operator around the square brackets, it somehow ...
0
votes
0
answers
652
views
Should spread operator behave differently in asynchronous conditions?
I am trying to replace an instance of hardcoding in a web application with 2 very similar API calls, as I don't have time to rewrite a whole new procedure and endpoint to handle the same functionality ...
0
votes
0
answers
53
views
Why is the spread operator needed to produce a return? [duplicate]
Trying to figure out why the spread operator is needed in the code below in order for it to produce a return. When I delete the ..., no return is produced.
function binaryAgent(str) {
return String....