How to push all the values in array? [duplicate] - javascript

This question already has answers here:
Copy array items into another array
(19 answers)
Closed 2 years ago.
I have this:
z: [[res.push(this.Map.heatmap[0])]],
so this is just one value. But how can I push all the values of the array
and this is the interface:
export interface map {
}
I have a array of 100 values in it:
10
but if I do this:
this.Map().subscribe((res) => {
zsmooth: 'best'
}
],
not all the values are loaded. So how to load all the values?
and this is how I have the object:
Map: map = {};
Thank you
Oke,
console.log(res)
gives back array with 100 values:
length: 100
but apperently this:
z: [[res]],
doesn't work. I dont see the value at all.
But if I do this:
hallo: console.log(res.push(this.Map.map[0])),
z: [[res.push(this.cMap.tmap[0])]],
it returns the number 2

concat function is maybe what you are looking for:
var array1 = [A, B, C];
var array2 = [D, E, F];
var newArray = array1.concat(array2);
The newArray will be [A,B,C,D,E,F]
In your case you would do something like:
z = z.concat(this.cameraAgretateHeadMap.heatmap)
a little bit more code from your side would have been helpful to understand it in a better what your problem is!
Hopefully this helps!

Related

get object value from an array [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 months ago.
Im quite new to front-end development. I am using React.
My problem:
How to extract object value that are within array?
myArr = [[{a: 1, b: 2}], [{a: 1, b:2}], [{a: 1, b:2}]]
// I want to extract only the values of b and make them into array
// my asnwer should look like this:
// [2,2,2]
My Approach:
const [answerArr, setAnswerArr] = useState([]);
useEffect(() => {
const extractCode = () => {
const res = myArr.map((item)=>{
????
})
};
extractCode();
}, [codeArr]);
I've tried using map method...but I am struggling very much...
If you could help me i would learn so much!
Your code should look something like this
const res = myArr.map(item => item[0].b)

js: add element at the start of an array [duplicate]

This question already has answers here:
How can I add new array elements at the beginning of an array in JavaScript?
(12 answers)
Closed 10 months ago.
Let´s say I have this array:
let array = ["somevalue", "anothervalue"]
So what I wanted to da was to add a value, for example:
let foo = {bar: true} and add this value at index 0, but not overwrite the old value, rather kind of 'push' the rest of the values back. I´ve heard of array = [foo, ...array] but I am not quite sure what it does and how and when to use it.
Anyone any ideas?
let array = ["somevalue", "anothervalue"]
let foo = {bar: true}
array.unshift(foo);
console.log(array); // prints [{bar: true}, "somevalue", "anothervalue"]
Similarly, the shift() method removes the first item in the array.
It simply append the foo in front of array. so array will become
[{bar: true}, "somevalue", "anothervalue"]
Alternate :
You can also use unshift, it also append data at index 0, eg.
let array = [2, 3, 4]
array.unshift(1)
console.log(array) /// output : [1, 2, 3, 4]
Ok,
let arr = [1,2]
let foo={bar:true}
arr=[foo].concat(arr)
also what you already know works
arr=[foo, ...arr]
That's it

How can I check if a value exists inside an array of objects in javaSript? [duplicate]

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 3 years ago.
For eg.
[
{
x:1,
y:2
},
{
x:10,
y:20
},
]
How can I check if x exists in both of the objects inside the array?
DESIRED OUTPUT:
if x doesn't exist in even one object inside the array ---> false
else ---->true
I have tried using the array.prototype.find() method but not able to find the right logic to get the desired output.
You could check the objects with Object.hasOwnProperty and the wanted property and take Array#every for checking all elements of the array.
var array = [{ x: 1, y: 2 }, { x: 10, y: 20 }],
result = array.every(o => o.hasOwnProperty('x'));
console.log(result);
You can use the operator in for checking if a property exists in the object, this along with the function every.
let arr = [{x:1,y:2},{x:10,y:20}];
console.log(arr.every((o) => "x" in o))
console.log(arr.every((o) => "z" in o))

"..." operator in Javascript [duplicate]

This question already has answers here:
What is SpreadElement in ECMAScript documentation? Is it the same as Spread syntax at MDN?
(3 answers)
Closed 6 years ago.
I found the following case in javascript. I don't understand '...' operator mean here. I search it on the Google, but I did not get anything about it. Is there any other usage for this operator? Can someone help me out?
var x= [1,2,3];
var y = [4,5,6];
var z = [...x, ...y]; // z will be [1,2,3,4,5,6];
Thanks.
I have a way of thinking which makes it very easy to understand and remember how '...' works.
var arr = [1,2,3] // this is arr which is the array
on the other hand
...arr // this is whatever inside arr, which is 1,2,3
So you can also think of it as taking what is inside of an array.
Note that by its own, ...arr is not a valid syntax. You can use it in
many ways , two of them coming to my mind are :
1 - Pass what is inside an array to a function
var arr = [ 1,2,3 ]
var myFunc = function(a,b,c) {
console.log(a,b,c)
}
myFunc(..arr) // logs 1 2 3
myFunc(1,2,3) // logs 1 2 3
2 - Take what is inside of an array and use them in another array.
var arr = [ 1,2,3 ]
var foo = [ ...arr, 4,5,6 ] // now foo is [ 1,2,3,4,5,6 ]

Filling an array with an identical function [duplicate]

This question already has answers here:
Functional approach to basic array construction
(5 answers)
Closed 8 years ago.
I'm trying to create a function that returns an array with n elements, that are all the same function (this array will later be used to call those functions in parallel using async).
I can easily loop over an array and add the function to each element, but was wondering if I can do it in one line, using map:
//the function to point to
var double = function(x) {
return x*2;
};
//this function will create the array - just a filler for a one-liner
var createConsumersArray = function(numOfConsumers) {
var consumers = (new Array(2)).map(function(x){return double;});
return consumers;
};
var t = createConsumersArray(2);
console.log(t); //prints [,]
console.log(t[1](2)); //TypeError: Property '1' of object , is not a function
If I pre-fill the array with constants, the map works, i.e.:
var x = [1,2,3];
console.log(x.map(function(x){return double;})); //prints [ [Function], [Function], [Function] ]
console.log(x[1](2)); //prints 4
How can I accomplish filling an array with an identical function in the shortest way?
You have to change a little.
var createConsumersArray = function(numOfConsumers) {
var consumers = Array.apply(null, Array(numOfConsumers)).map(function(){return double;});
return consumers;
};
This is more functional programming. If you'd like to program in this style, I'd recommend you look at underscore.js. Here's an example of a range function:
_.range(10);
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
For your use case you would do:
_.map(_.range(4), function(num){ return double; });
Here's the corresponding jsfiddle example:

Categories

Resources