How to concatenate and use spread operator in dart [duplicate] - javascript

This question already has answers here:
How to flatten a List?
(6 answers)
Closed 2 years ago.
This is js code of doing it!
nums = [].concat(...digitBuckets);
how can i implement it in dart?
nums=[].addAll(...digitBucketsd);//facing problem here and confused

The spread operator is designed to insert array elements into another array or to map its elements to function arguments.
The mistake is that: elements of array are used as arguments of concat function, but concat function requires array as argument but not its elements as arguments:
replace
nums = [].concat(...digitBuckets);
nums = [].addAll(...digitBucketsd);
with
nums = [].concat(digitBuckets);
nums = [].addAll(digitBucketsd);
or with spread
nums = [...digitBuckets];
nums = [...digitBucketsd];
also digitBucketsd is present in question instead of digitBuckets

Related

How to deconstruct arrays of arrays in Javascript [duplicate]

This question already has answers here:
JavaScript flattening an array of arrays of objects
(14 answers)
Closed 4 months ago.
I'm working with an API and after I try to clean up the data, I got an array of arrays of arrays:
arr = [[[{name: "john"}],[{name: "jack"}]],[[{name: "joe"}],[{name: "bob"}]]]
How can I clean this up to something like this:
arr = [{name: "john"},{name: "jack"},{name: "joe"},{name: "bob"}]
You can use Array.prototype.flat(), providing Infinity as the depth argument to flatten all sub-arrays recursively:
const arr = [[[{name: "john"}],[{name: "jack"}]],[[{name: "joe"}],[{name: "bob"}]]];
const flattened = arr.flat(Infinity);
console.log(flattened);
In this case calling arr.flat().flat() would do the trick.

How do I fill a new array with n distinct empty arrays, in a concise one-line initialization statement? [duplicate]

This question already has answers here:
How do you easily create empty matrices javascript?
(17 answers)
fill an array with arrays programmatically
(5 answers)
Array.prototype.fill() with object passes reference and not new instance
(7 answers)
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 1 year ago.
Here is where I am stuck. I want to take this statement and revise it in a manner that the empty array I fill (which I surmise might not work with dynamic values), will initialize bucket to the n distinct empty arrays.
How do I do this? Is there a way to make this fill method behave in the intended manner?
let radix = 10;
let badBucket = [...Array(radix).fill([])];
let goodBucket = JSON.parse(JSON.stringify([...Array(radix).fill([])]));
badBucket[3].push(33);
goodBucket[3].push(33);
console.log(JSON.stringify(badBucket));
console.log(JSON.stringify(goodBucket));
You can use the callback function of Array.from.
let length = 5;
let res = Array.from({length}, _=>[]);
console.log(res);
Try:
let radix = 10;
let bucket = [...Array(radix)].map(e=>[])
bucket[0].push(1)
console.log(JSON.stringify(bucket));

Flatten array with arrays [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 3 years ago.
Say I have an array with arrays inside it. I would like to flatten it and get a single array with all the values.
let arrWithArrs = [[1,2,3], [4,5,6]];
let array = arrWithArrs.map(arr => ...arr);
This obviously doesn't work but I would like to know how to make it work.
The wanted outcome would be
array = [1,2,3,4,5,6];
You can perform a reduce on the array to combine each of the arrays into a single array like so:
arrWithArrs.reduce((result, lst) => [...result, ...lst], []);

Javascript +ES: Is there any difference between [...arr] and arr? [duplicate]

This question already has answers here:
What is the difference when we use array names instead of spread operator?
(3 answers)
Closed 4 years ago.
Consider a scenario where in you are trying to iterate over a const array.
Would there be any difference between
[...arr].forEach((elem) => {
// your operations
});
and
arr.forEach((elem) => {
// your operations
});
Can these two be used interchangeably?
It will make a copy of the array. If the callback function would use the array parameter and it would modify it, that would make a difference. E.g.:
[...arr].forEach((val, i, a) => a.push(val))
If arr is not an array but an array-like, it will turn it into an array and thereby allow you to forEach over it. E.g.:
[...document.getElementsByTagName('p')].forEach(p => console.log(p))

I have my array as arr=[1,2,3,4]. I pushed an element in it arr.push(5). Now I need to display my array as [5,4,3,2,1]. Any Idea? [duplicate]

This question already has answers here:
How can I reverse an array in JavaScript without using libraries?
(36 answers)
Closed 7 years ago.
I have an array as:
var arr = [1,2,3,4]
//Push and element to array
arr.push(5)
//Now, arr = [1,2,3,4,5]
I need to display my array as
Elements in array arr is:
5,1,2,3,4
Arr.reverse() gives me
5,4,3,2,1.
But i need
5,1,2,3,4
Simply use Array.prototype.reverse():
console.log(arr.reverse());
References:
Array.prototype.reverse().
This is the code you need to use :
arr.reverse();
Here is the w3school : http://www.w3schools.com/jsref/jsref_reverse.asp

Categories